Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Parameter Passing with Laravel

Tags:

php

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);
});
like image 886
Akash Avatar asked Jun 21 '12 18:06

Akash


2 Answers

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

Great to see you using Laravel!

like image 109
daylerees Avatar answered Oct 26 '22 12:10

daylerees


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);
}
like image 36
NIKHIL NEDIYODATH Avatar answered Oct 26 '22 11:10

NIKHIL NEDIYODATH