8

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);

}
jrsalunga
  • 409
  • 2
  • 8
  • 20
  • 1
    pass it to the controllers method as a parameter `index($page)` – Jeemusu Jul 08 '15 at 03:04
  • i've already done it! but thanks for this i've figured it out the real problem with this error `Declaration of App\Http\Controllers\PageController::index() should be compatible with App\Http\Controllers\Controller::index()` – jrsalunga Jul 08 '15 at 03:53

2 Answers2

8

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'
]);
Jeemusu
  • 10,415
  • 3
  • 42
  • 64
0

in my case, showing specified user, it's pretty much the same case

route file (web.php)

Route::get('user/{id}/show', ['as'=> 'show', 'uses'=>'UserController@show']);

im still using facade

view file (users.blade.php)

href="{{route('show', ['id' => $user->id])}}"

just passing an array to route name and the last one in

controller file(UserController.php)

public function show($id)
    {
        $user = User::findorfail($id)->first();
        return view('user', compact('user'));
    }

it's done