Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen: how to pass parameters to controller from route

Tags:

laravel

lumen

from my route i need to pass the value of $page to controller

route:

$app->get('/show/{page}', function($page) use ($app) {

  $controller = $app->make('App\Http\Controllers\PageController');
  return $controller->index();

});

controller:

public static function index(){

  /** how can I get the value of $page form here so i can pass it to the view **/

  return view('index')->with('page', $page);

}
like image 775
jrsalunga Avatar asked Jul 08 '15 03:07

jrsalunga


1 Answers

You could pass it as a parameter of the index function.

Route

$app->get('/show/{page}', function($page) use ($app) {
    $controller = $app->make('App\Http\Controllers\PageController');
    return $controller->index( $page );
});

Although the route looks wrong to me, normally you define the route without a forward slash at the beggining: $app->get('show/{page}', ....

Controller

public static function index($page)
{
    return view('index')->with('page', $page);
}

Unless there is a reason for using a closure, your route could be re-written as below, and the {$page} variable will automatically be passed to the controller method as a parameter:

Route

$app->get('show/{page}', [
    'uses' => 'App\Http\Controllers\PageController@index'
]);
like image 123
Jeemusu Avatar answered Jan 21 '23 15:01

Jeemusu