How to write laravel 5.8 controllers routes similar to laravel 5.1

773 Views Asked by At

Having more than 20 controllers. It's very difficult to set each and every routes for add, edit and delete (also having more actions).

This is my laravel 5.1 routes.php :

Route::controllers([
  'user' => 'UserController',
  'taxes' => 'TaxController',
]);

Is there any way to support these routes in laravel 5.8?

3

There are 3 best solutions below

7
Lizesh Shakya On

You can use the Resource Controller and implement in routes/web.php. It will autogenerate the name for the route

//web.php

Route::resource('user', 'UserController'); 
Route::resource('taxes', 'TaxController'); 

Resource Controller with Action

Edit 1

If you want to exclude show method of the controller for the resource, You can add array inside the except method.

Route::resource('taxes', 'TaxController', [
    'except' => ['show']
]);

Further, if you want to get only selected options, You can use only.

Route::resource('taxes', 'TaxController', [
    'only' => ['index', 'create', 'store', 'edit']
]);
2
George Hanson On

The controllers method was deprecated in Laravel 5.2. From the upgrade guide:

Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file.

1) Use Resource Routes

Provided that your controllers use the standard index, store, show etc methods you can simply use resource routes. For example:

Route::resource('user', 'UserController');

However if you want to exclude certain methods you can add them to the resource. For example:

Route::resource('user', 'UserController', ['except' => 'show']);

2) Declare Routes Explicitly

You can follow the Laravel 5.2 upgrade guide as above and instead declare each route explicitly.

3) Create a Macro

The Laravel router is Macroable. This means that you can add your own methods to it. For example, in your app service provider you could have the following:

Illuminate\Routing\Router::macro('controllers', function ($routes) {
    // Create your own implementation of the controllers method.
});

This allows you to create your own implementation of the controllers method which means you wouldn't need to alter your routes or controllers, but you may need to dive in and look at Laravel's route handling to understand how to implement this.

I hope this helps.

0
Rohit Agrohia On

You can use in the array, In as you call using routes. like {{route('claimsubmit')}}

Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController@claimform');