How to get URI parameters in methods inside a implicit controller?
First, I define a base route:
Route::controller('users', 'UserController');
Then,
class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
If I want to pass aditional parameters in URI, like http://myapp/users/{param1}/{param2}
, how can I read param1
and param2
inside the respectve method? In this example, getIndex()
If you want to have URL like http://myapp/users/{param1}/{param2}
you need to have in your controller like this:
Route::get('users/{param1}/{param2}', 'UserController@getIndex');
and access it:
class UserController extends BaseController {
public function getIndex($param1, $param2)
{
//
}
}
but hey, you can also do something like this, the routes will be same:
class UserController extends BaseController {
public function getIndex()
{
$param1 = Input::get('param1');
$param2 = Input::get('param2');
}
}
but your URL would be something like: http://myapp/users?param1=value¶m2=value
Here is a way of creating a directory like hierarchy between models (nested routing)
Assume Album has Images, we would like an album controller (to retrieve album data) and an image controller (to retrieve image data).
Route::group(array('before' => 'auth', 'prefix' => 'album/{album_id}'), function()
{
// urls can look like /album/145/image/* where * is implicit in image controller.
Route::controller('image', 'ImageController');
});
Route::group(array('before' => 'auth'), function()
{
// urls can look like /album/* where * is implicit in album controller.
Route::controller('album', 'AlbumController');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With