Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 6.0 How to use Route:resource get route same folder controller

Tags:

I have question and I'm can't find a solution in document

I use command

php aritsan make:controller Backend\ProductController --resource --Model=Model\Product

So, I will need route same location file controller

I use

Route::resource('/backend/product','Backend\ProductController');

after, run a command

php artisan route:list

and this result enter image description here

But, I don't need this I think should be

+--------+-----------+----------------------------------+-----------------+------------------------------------------------------------+------------+
| Domain | Method    | URI                              | Name            | Action                                                     | Middleware |
+--------+-----------+----------------------------------+-----------------+------------------------------------------------------------+------------+
|        | GET|HEAD  | backend/product                  | backend.product.index   | App\Http\Controllers\Backend\ProductController@index       | web        |
|        | POST      | backend/product                  | backend.product.store   | App\Http\Controllers\Backend\ProductController@store       | web        |
|        | GET|HEAD  | backend/product/create           | backend.product.create  | App\Http\Controllers\Backend\ProductController@create      | web        |

Route name should be backend.product.index

I find a solution. but not happy.

Route::resource('/backend/user','Backend\UserController')->names([
    'index' => 'backend.user.index',
    'store' => 'backend.user.store',
    'edit' => 'backend.user.edit',
    'update' => 'backend.user.update',
    'destroy' => 'backend.user.destroy',
]);

Documents resource names

like image 823
story ks Avatar asked Oct 09 '19 10:10

story ks


People also ask

What does route :: resource do in Laravel?

Route::resource: The Route::resource method is a RESTful Controller that generates all the basic routes required for an application and can be easily handled using the controller class.

What are routes in Laravel explain Route handling with controller and route parameters?

You can define a route to this controller action, as: Route::get('user/{id}', 'UserController@show'); Route::resource: The Route::resource method can be a Restful Controller that produces all the basic routes required for an application and is dealt via controller class.

How do you make a resource controller in Laravel?

Creating the Controller This is the easy part. From the command line in the root directory of your Laravel project, type: php artisan make:controller sharkController --resource This will create our resource controller with all the methods we need.

Where is the routes folder in Laravel?

All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider . The routes/web.php file defines routes that are for your web interface.


1 Answers

The command for creating Model Controller with resource

php artisan make:controller Backend\ProductController --resource --Model=Model\Product

Change web.php and use prefix, namespace, as

Route::group(['prefix' => 'backend','namespace'=>'Backend','as'=>'backend.'], function () {
    Route::resource('product','ProductController');
});

Now use

php artisan route:list

backend.product.index

backend.product.create

backend.product.show

backend.product.destroy

backend.product.update

backend.product.edit

like image 61
Dilip Hirapara Avatar answered Oct 18 '22 13:10

Dilip Hirapara