harikrish
harikrish

Reputation: 2019

laravel 4 routing passing values to controller

I have a route as below

Route.php

Route::get('u/list/{type}', 'UsersController@listAll');

UsersController.php

class UsersController extends BaseController 
{
  public function listAll($id)
    {
    $users = User::all();
    return Response::json( $users );
    }
}

this works well, i can pass the type into the listAll() method

Now i have another url which is like this

  Route::get('u/doctor', 'UsersController@listAll');
  Route::get('u/receptionist', 'UsersController@listAll');

This is equivalent to

  Route::get('u/list/1', 'UsersController@listAll');
  Route::get('u/list/2', 'UsersController@listAll');

is there a way for the URL "u/doctor" to pass the number 1 to listall() method

Or else i will have to rewrite the function with a new name.

Upvotes: 0

Views: 24

Answers (1)

Marwelln
Marwelln

Reputation: 29413

Use a closure instead of a string.

Route::get('u/doctor', function(){
    return (new UsersController)->listAll(1);
});

Upvotes: 1

Related Questions