3

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?

Komal
  • 2,716
  • 3
  • 24
  • 32
  • Not a duplicate, but probably answered here: https://stackoverflow.com/questions/23505875/laravel-routeresource-vs-routecontroller – Fitzi Jun 26 '19 at 08:11
  • Please do not do this. Be explicit with your routes. If you must do this then [the source is here](https://github.com/laravel/framework/blob/5.1/src/Illuminate/Routing/Router.php#L232) which relies on [this](https://github.com/laravel/framework/blob/5.1/src/Illuminate/Routing/ControllerInspector.php) – apokryfos Jun 26 '19 at 08:21
  • @apokryfos : Okay – Komal Jun 26 '19 at 08:24

3 Answers3

4

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']
]);
Lizesh Shakya
  • 2,482
  • 2
  • 18
  • 42
  • 1
    It should be `routes/web.php` instead of `api/web.php`. – sujeet Jun 26 '19 at 08:13
  • Thanks for replay.. I tried this, but required show method. I got error App\Http\Controllers\UserController::show does not exist. – Komal Jun 26 '19 at 08:16
  • 1
    @Komal You have to make a resource controller using `php artisan make:controller TaxesController --resource`. It will give you a nice CRUD boilerplate. – sujeet Jun 26 '19 at 08:18
  • Basically I m converting my 5.1 project into 5.8, I don't want to change my code.. Working on big project 80% completed but for one feature I m switching to laravel 5.8 – Komal Jun 26 '19 at 08:19
  • You can explicitly define which functions of the controller to be used in the resource. I have edited my answer above. – Lizesh Shakya Jun 26 '19 at 08:20
  • Means I have to set all routes :( right.. I m just trying to avoid my 100 line code for route – Komal Jun 26 '19 at 08:23
  • 1
    A resource controller is not the same as any controller. This answer will only work if OPs controllers were resource controllers, and we don't have any indication that they are. – apokryfos Jun 26 '19 at 08:23
3

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.

Community
  • 1
  • 1
George Hanson
  • 2,940
  • 1
  • 6
  • 18
2

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

Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController@claimform');
Rohit Agrohia
  • 368
  • 3
  • 7