0

sorry i'm a laravel newbie - here's my route code:

Route::get('shop/{id}', function($id)
{
});

$id represents the shop category id and i would like to pass id to a controller-action like shop\StartController@showarticles

i'm only used to this syntax:

Route::get('/', 'shop\StartController@show');

how would i do that? thanks

2
  • It can be as simple as this: Route::get('shop/{id}', array('as' => 'shop', 'uses' => 'ShopController@index')) Commented Sep 30, 2015 at 7:58
  • You can write it the same way as your main, no need of function(){} Commented Sep 30, 2015 at 8:03

4 Answers 4

3

Just pass it as a parameter like this (assuming Laravel 5.1 is used):

Controller:

class StartController extends Controller
....
public function show($id)
{
   //add controller logic
}

Routes:

//Asuming that the namespace `shop` is loaded
Route::get('/', 'StartController@show');

Check out more here

Sign up to request clarification or add additional context in comments.

Comments

2

you can easily pass the id to controller

Route::get('shop/{id}','UserController@show');

Your controller

class UserController extends BaseController {


public function show($id)
{
    echo "hi its my first exercise in laravel".$id;
}

Comments

0

You can use closure for your route and call the controller like this:

Route::get('shop/{id}', function($id)
    return App::make('StartController')->showarticles($id);
}));

Comments

0

Your controller would be like

Route::get('shop/{id}', 'shop\StartController@showarticles');

and your class function would be like

class StartController extends Controller
....
public function show($id)

or incase you are using latest version

class StartController extends Controller
....
public function show(Request $request, $id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.