Laravel 4: Route to localhost / controller / action

I'm more or less new to Laravel 4. I have never used routes before, but usually I use url / controller / action and then backend routing for me. I read the documentation for routes and controllers several times, and also read some lessons, and so I am trying to figure out how to make this work without naming the route for each controller and actions.

I tried something like

Route::get('{controller}/{action}', function($controller, $action = 'index'){ return $controller."@".$action; }); 

So, I know this is wrong because it does not work, but what am I missing? In most study guides, etc. I see a route for more or less each controller and actions like:

 Route::get('/controller/action' , ' ControllerName@Action '); 

Which seems stupid and a waste of time for me.

Anyway, to achieve what I want?

+6
source share
3 answers

If you are looking for more automatic routing, this would be the Laravel 4 way:

Route:

 Route::controller('users', 'UsersController'); 

Controller (in this case UsersController.php):

 public function getIndex() { // routed from GET request to /users } public function getProfile() { // routed from GET request to /users/profile } public function postProfile() { // routed from POST request to /users/profile } public function getPosts($id) { // routed from GET request to: /users/posts/42 } 

As mentioned in Shift Exchange, there are some advantages to doing this in the right way. In addition to the excellent article he linked, you can create a name for each route , for example:

 Route::get("users", array( "as"=>"dashboard", "uses"=>" UsersController@getIndex " )); 

Then, when creating URLs in your application, use the helper to create a link along the specified route :

 $url = URL::route('dashboard'); 

Then links will be checked in the future from changes in controllers / actions.

You can also create links directly to actions that will continue to work with automatic routing.

 $url = URL::action(' UsersController@getIndex '); 
+7
source
  app \
     controllers \
         Admin \
            AdminController.php
         IndexController.php
 Route::get('/admin/{controller?}/{action?}', function($controller='Index', $action='index'){ $controller = ucfirst($controller); $action = $action . 'Action'; return App::make("Admin\\{$controller}Controller")->$action(); }); Route::get('/{controller?}/{action?}', function($controller='Index', $action='index'){ $controller = ucfirst($controller); $action = $action . 'Action'; return App::make("{$controller}Controller")->$action(); }); 
+6
source

I come from the world of .Net and routing is usually done:

 /{Controller}/{action}/{id} 

What does it look like:

 /Products/Show/1 OR /Products/Show/Beverages 

In Laravel, I do this routing like this:

 Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='index', $id = null) { $controller = ucfirst($controller); return APP::make("{$controller}Controller")->$action($id); }); 

The controller will look something like this:

 class ProductsController extends BaseController { public function Show($id) { $products = array( 1 => array("Price" => "$600","Item" => "iPhone 6"), 2 => array("Price" => "$700", "Item" => "iPhone 6 Plus") ); if ($id == null) { echo $products[1]["Item"]; } else { echo $products[$id]["Item"]; } } } 
0
source

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


All Articles