How to add a dash between controller names in Cohan?

I am working on an authorization module for Kohana 3.1. In my init.php module ...

Route::set(

    'a11n',
    '<controller>',
    array(
        'controller' => 'signIn|signOut|signUp'
    )

);

I am not 100% sure how to use the Kohana routing mechanism, but with this I am trying to ensure that the user can enter "signIn", "signOut" or "signUp" to start the controllers from my module. You see, I have a "portable" authorization system ... so I can just "copy-paste" the right directory, allow authorization of the module and my site.

Keep in mind that with this route I don’t want to change the default behavior of routes. I don’t know how correct my code is ... but it works! I tested, and I can get the same effect without using the third parameter. What can I achieve with this now?

And now the question is ... How can I somehow set the routes from the module by typing the "custom" custom launcher module "Controller_SignIn"?

+3
source share
3 answers

You should use routes for this, something like this:

Route::set('SignIn', '/sign-in(/<action>)',
        array(
            'action' => 'index|action1',
            )
        )
        ->defaults(
                array(
                    'controller' => 'SignIn',
                    'action' => 'index',
                    )
                );

Route::set('SignOut', '/sign-out(/<action>)',
        array(
            'action' => 'index|action1',
            )
        )->defaults(
                array(
                    'controller' => 'SignOut',
                    'action' => 'index',
                    )
                );

or

Route::set('SignIn', '/sign-in/',
        array()
        )
        ->defaults(
                array(
                    'controller' => 'user',
                    'action' => 'login',
                    )
                );

Route::set('SignOut', '/sign-out/)',
        array()
        )->defaults(
                array(
                    'controller' => 'user',
                    'action' => 'logout',
                    )
                );
+4
source

I know that there is already an answer in this question, designated as a solution, but there is a cleaner / different way to do this:

Create a new file in the application: application/classes/request.phpand put the following code in this file:

<?php defined('SYSPATH') or die('No direct script access.');
class Request extends Kohana_Request
{
    public function execute()
    {
        $this->action(str_replace('-', '', $this->action()));
        $this->controller(str_replace('-', '', $this->controller()));
        return parent::execute();
    }
}

/ bootstrap.php / URL!

+3

? (Controller_Account - ) :

class Controller_Account extends Controller_Template {

    public function action_signin() {...}

    public function action_signout() {...}

    public function action_signup() {...}

}

As you can see, action names do not have a dash. You cannot use them in method names. But here is to crack it:

public function before()
{
    parent::before(); // dont forget this call!
    // remove dashes from current method name
    $this->request->action(str_replace('-', '', $this->request->action()));
}

And the route:

Route::set(
       'a11n', 
       '<action>', 
       array('action' => array('sign-in|sign-up|sign-out'))
    )
    ->defaults(array('controller' => 'account'));

Of course, you can use both signin and sign-in names, just add non-empty names to the Route regular expression parameter:

Route::set(
       'a11n', 
       '<action>', 
       array('action' => array('sign-in|sign-up|sign-out|signin|signup|signout'))
    )
    ->defaults(array('controller' => 'account'));
+1
source

All Articles