5

I am working on Kohana PHP framework.

I want to show a 'username' instead of controller name in my URL.

For example,

username = james then how to show

http://localhost:3000/james

instead of

http://localhost:3000/scrapbook/index => ... localhost:3000/scrapbook

(controller: scrapbook, action: index)

in the url.

My bootstrap file have the entry for such types of url. If I manually write ..//localhost:3000/james, it takes me to the requested page.

//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

Kowser
  • 8,123
  • 7
  • 40
  • 63
user990479
  • 115
  • 2
  • 8
  • What have you tried so far? Just asking, because normally this should be documented in the kohana framework. That's called routing if it helps: http://kohanaframework.org/3.0/guide/kohana/routing – hakre Oct 31 '11 at 23:07
  • I have the routing shown below. But when I manually login by james userid, i want to show james in the URL. I havent started anything on this. THinking what I should change so that it will replace my scrapbook controller with the username... //Viewing a user's profile or account details - user/action Route::set('profile', '((/(/)))', array( 'username' => '([A-Za-z0-9\-]+)')) ->defaults(array( 'controller' => 'scrapbook', 'action' => 'index')); – user990479 Oct 31 '11 at 23:21
  • I did pasted in the question since i cannot answer my own question. Thanks – user990479 Oct 31 '11 at 23:28
  • If I understand your question correctly, your problem is more on generating the custom URL to be put in the `` rather than making Kohana understand the custom URL. How do you generate the URL? Do you use `URL::base()` or `Route::uri()`? – Lukman Nov 01 '11 at 13:05
  • Hi Lukman, I am new to this framework. As far as I know, i m setting up the URL in the bootstrap.php as shown above. Can you please let me know, how can I check the way I am creating the URL? – user990479 Nov 02 '11 at 20:09

1 Answers1

3

When you complete your sign in action, you'll want to redirect the user to the desired URL using reverse routing:

// ...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.

David Winter
  • 196
  • 5
  • Hi david, I tried above code into my scrapbook controller when user actually sign in, but it still shows localhost/XYZ/scrapbook and not replacing 'scrapbook' word by the name of the 'username'. I need to show user their username in the URL like twitter or other websites shows. – user990479 Nov 02 '11 at 20:07
  • Can you paste in your code for what happens after someone signs in and you redirect them? Also, has the route that you included in your question changed at all? – David Winter Nov 04 '11 at 10:44
  • Hi David, sorry for the late reply. It worked. My routing was correct, but I was messing at some other place. I used the same routing as shown in my question. – user990479 Nov 11 '11 at 02:12