开发者

How To Add Dash Between Controllers Name In Kohana?

开发者 https://www.devze.com 2023-03-10 16:22 出处:网络
I\'m working on authorization module for Kohana 3.1. In my module\'s init.php... Route::set( \'a11n\', \'<controller>\',

I'm working on authorization module for Kohana 3.1. In my module's init.php...

Route::set(

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

);

I'm not 100% sure how use to Kohana's routing mechanism, but with this I'm trying to achieve that user can type "signIn", "signOut" or "signUp" to run controllers from my module. You see, I want have "portable" authorization system... so I can simply "copy-paste" right directory, enable the module and my site have authorization.

Keep in mind, with this route I don't want to anyhow change behav开发者_JS百科ior of default routes. I don't know how correct my code is... but it works! I tested and I can get the same effect without using 3rd parameter too. What do I achieve with it now?

And now the question... How can I somehow set routes from module that by typing "sign-in" user run module "Controller_SignIn"?


You should use the routes to do that, 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',
                    )
                );


I know this question already has an answer marked as solution, but there is a cleaner/another way of doing it:

Create a new file in your application: application/classes/request.php and put the following code in that 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();
    }
}

Now you don't have to modify/pollute your bootstrap.php for every dashed/hyphenated url!


Why do you create separated controllers for account actions? Create one controller (Controller_Account or something else) with actions you need:

class Controller_Account extends Controller_Template {

    public function action_signin() {...}

    public function action_signout() {...}

    public function action_signup() {...}

}

As you can see, action names are without dashes. You cant use them in method names. But here is a hack for 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 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-dashed names to Route regex param:

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

精彩评论

暂无评论...
验证码 换一张
取 消