controller
1. General controller
1.php artisan命令
php artisan make:controller UserController
2. Modify the controller file usercontroller.php
Add the show method
public function show(){ dd('I am Liang Jing'); }
3. Web.php modify
the reference namespace
use App\Http\Controllers\UserController;
4. Set the route
Route::get('/user',[UserController::class,'show']);
【Picture part】
2. Resource Controller
1. Create a resource controller
php artisan make:controller UserController --resource
2. Modify the method content in the controller
dd('index');
3. Routing
Resource routing
Route::resource('/user',UserController::class );
– View the added route
user/create
user with parameters
user/{user}/edit
3. Partial resource routing
Routing model binding
php artisan make:controller PhotoController --resource --model=Photo
[API Resource Routing] Parameters
used in the command : php artisan make:controller PhotoController --api without create, edit--api
4. Resource routing naming
Route::resource('photos', PhotoController::class)->names([
'create' => 'photos.build'
//可以传入 `names` 数组来覆盖路由名称
]);
For example:
Route::resource('/user',UserController::class)->names(['index'=>'allData']);
Supplementary Resource Controller
If you need to add additional routes to the default resource routes, you need to define them Route::resource
before the ; otherwise, resource
the routes defined by the method may inadvertently take precedence over the routes you define:
if you put the supplementary routes in Route:: After resource is defined, info will be used as the parameter of show to match, and info will not be called.