简体   繁体   English

如何在Kohana PHP Framework中的url路由中显示用户名?

[英]How to show username in the url route in Kohana PHP Framework?

I am working on Kohana PHP framework. 我正在研究Kohana PHP框架。

I want to show a 'username' instead of controller name in my URL. 我想在我的网址中显示“用户名”而不是控制器名称。

For example, 例如,

username = james then how to show username = james然后如何显示

http://localhost:3000/james HTTP://本地主机:3000 /詹姆斯

instead of 代替

http://localhost:3000/scrapbook/index => ... localhost:3000/scrapbook http:// localhost:3000 / scrapbook / index => ... localhost:3000 /剪贴簿

(controller: scrapbook, action: index) (控制器:剪贴簿,动作:索引)

in the url. 在网址中。

My bootstrap file have the entry for such types of url. 我的引导程序文件包含此类URL的条目。 If I manually write ..//localhost:3000/james, it takes me to the requested page. 如果我手动编写..//localhost:3000/james,它会将我带到请求的页面。

//Viewing a user's profile or account details - user/action
Route::set('profile', '(<username>(/<action>(/<id>)))',
    array(
        'username'   => '([A-Za-z0-9\-]+)'))
    ->defaults(array(
        'controller' => 'scrapbook',
        'action'     => 'index'));

What I want is if I manually signin and go to scrapbook, my url should show 'username' and not the name of the controller. 我想要的是如果我手动登录并转到剪贴簿,我的网址应该显示'用户名'而不是控制器的名称。 I will appreciate if anyone can guide me in this. 如果有人能指导我,我将不胜感激。 Thanks 谢谢

When you complete your sign in action, you'll want to redirect the user to the desired URL using reverse routing: 完成登录操作后,您需要使用反向路由将用户重定向到所需的URL:

// ...in your controller
function action_signin()
{
    // ...sign in logic

    $this->request->redirect(
        Route::get('profile')->uri(array(
            'username' => $username
        ))
    );
}

$username will be whatever the username of the user of the logged in user is that just signed in. $username将是刚刚登录的登录用户的用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM