Akash
Akash

Reputation: 5012

URL Parameter Passing with Laravel

I want to visit a page like...

http://mysitelocaltion/user_name/user_id

This is just a virtual link, I have used a .htaccess -rewrite rule to internally pass "user_name" and "use_id" as get parameters for my actual page.

How do I achieve the same in Laravel?

Update: This shall help (documentation)

Route::get('user/(:any)/task/(:num)', function ($username, $task_number) {
    // $username will be replaced by the value of (:any)
    // $task_number will be replaced by the integer in place of (:num)
    $data = array(
        'username' => $username,
        'task' => $task_number
    );
    return View::make('tasks.for_user', $data);
});

Upvotes: 10

Views: 11585

Answers (2)

NIKHIL NEDIYODATH
NIKHIL NEDIYODATH

Reputation: 2932

You can add the following in your route

Route::get('user_name/{user_id}', 'YourControllerName@method_name');

In your controller you can access the value as follows

public function method_name(Request $request, $user_id){
    echo $user_id;
    $user = User::find($user_id);
    return view('view_name')->with('user', $user);
}

Upvotes: 1

daylerees
daylerees

Reputation: 1006

Route::get('(:any)/(:any)', function($user_name, $user_id) {
    echo $user_name;
});

Great to see you using Laravel!

Upvotes: 13

Related Questions