Laravel 4 - URI parameters in implicit controllers

How to get URI parameters in methods inside an implicit controller?

First, I define the 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 additional parameters to a URI, for example http://myapp/users/{param1}/{param2} , how can I read param1 and param2 inside the respectve method? In this example, getIndex ()

+5
source share
2 answers

If you want to have a URL, for example http://myapp/users/{param1}/{param2} you need to have the following in your controller:

 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 the same:

 class UserController extends BaseController { public function getIndex() { $param1 = Input::get('param1'); $param2 = Input::get('param2'); } } 

but your url will look something like this: http://myapp/users?param1=value¶m2=value

+7
source

Here's a way to create a directory, such as a hierarchy between models (nested routing)

Suppose an album has images, we need an album controller (to get album data) and an image controller (to get 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'); }); 
+2
source

Source: https://habr.com/ru/post/1205652/


All Articles