【Laravel】--Controller

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】
insert image description here

insert image description here

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
insert image description here
user/create

insert image description here
user with parameters
insert image description here

user/{user}/edit
insert image description here

3. Partial resource routing

Routing model binding
php artisan make:controller PhotoController --resource --model=Photo
insert image description here

insert image description here
Please add a picture description
Please add a picture description
insert image description here
[API Resource Routing] Parameters
used in the command : php artisan make:controller PhotoController --api without create, edit--api


insert image description here

4. Resource routing naming

Route::resource('photos', PhotoController::class)->names([
    'create' => 'photos.build'
    //可以传入 `names` 数组来覆盖路由名称
]);

For example:

Route::resource('/user',UserController::class)->names(['index'=>'allData']);

insert image description here

Supplementary Resource Controller

If you need to add additional routes to the default resource routes, you need to define them Route::resourcebefore the ; otherwise, resourcethe routes defined by the method may inadvertently take precedence over the routes you define:
insert image description here
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.
insert image description here

Guess you like

Origin blog.csdn.net/lj1641719803hh/article/details/123578635