当前位置:网站首页>Laravel document sorting 4. Controller
Laravel document sorting 4. Controller
2022-06-25 04:19:00 【Angry devil】
Preface :Laravel Document sorting , Only for record , Nothing else .
1、 What the most basic controller looks like
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Display the personal data of the specified user .
*
* @param int $id
* @return Response
*/
public function showProfile($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
Be careful : All controllers should inherit from the underlying controller , Because the basic controller is related to the core of the framework , let me put it another way , Your controller does not inherit from the underlying controller , Your business logic won't work .
Define a route , Point to the method corresponding to this controller :
Route::get('user/{id}', '[email protected]');
2、 When defining the route of the controller , Whether the full controller namespace needs to be specified ?
Unwanted
Such as App\Http\Controllers\Photos\AdminController
Just define Photos\AdminController This part is OK
example :
Route::get('foo', 'Photos\[email protected]');
Ps: reason , By default ,RouteServiceProvider Will use routing groups , hold routes.php All routes in the file are configured to the namespace of the root controller
3、 How to name the route of the controller ?
Route::get('foo', ['uses' => '[email protected]', 'as' => 'name']);
Compare the basic :
Route::get('foo', 'Photos\Admi[email protected]');
4、 With this route name , What's the use ?
such as , You have to pass the name , Know the route of the controller url, So at this point , The name of the controller route is used :
$url = route('name');
5、 This is the only way to get the controller route url Do you ?
No
The following can also be :
$url = action('[email protected]');
6、 How to get the behavior name of the controller in progress ?
$action = Route::currentRouteAction();
7、 How to specify middleware for routing
Route::get('profile', [
'middleware' => 'auth',
'uses' => '[email protected]'
]);
8、 How to specify middleware accurately ?
Specify... In the constructor .
example :
class UserController extends Controller
{
/**
* Add one UserController example .
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('log', ['only' => ['fooAction', 'barAction']]);
$this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}
}
such , You can do it , In the current class , What methods , Which middleware to use , More flexible and simple , If you define in the route , Then you have to write every route .
9、 What is a resource controller ?
php artisan make:controller PhotosController
The above command , Can be generated quickly PhotosController Controller files :
app/Http/Controllers/PhotosController.php
also , In this file , It also includes conventional methods such as addition, deletion, modification and query
Next , You can register resourced routes in the controller :
Route::resource('photos', 'PhotosController');
This route declares , Multiple routes will be created .
The behavior handled by the resource controller :
10、 When declaring a resource route , You can also specify the routing action
Route::resource('photos', 'PhotosController',
['only' => ['index', 'show']]);
or
Route::resource('photos', 'PhotosController',
['except' => ['create', 'store', 'update', 'destroy']]);
11、 How to route duplicate names to resources
Route::resource('photos', 'PhotosController',
['names' => ['create' => 'photo.build']]);
Ps: By default, resource controller behaviors have names , however , You can also pass a in the options names Array , Override name
Route::resource('photos', 'PhotosController',
['names' => ['create' => 'photo.build']]);
12、 What is nested resource ?
For example, multiple comments may be nested in Photo resources , In the code , How to achieve ?
Used in route declaration [ spot ]
Route::resource('photos.comments', 'PhotoCommentController');
This route will register a nested resource , visit url
photos/{photos}/comments/{comments}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class PhotoCommentController extends Controller
{
/**
* Show comments for the specified photo .
*
* @param int $photoId
* @param int $commentId
* @return Response
*/
public function show($photoId, $commentId)
{
//
}
}
See the parameters , All the way clear .
13、 How to attach a resource controller ?
This problem , In plain English , Resource controller , By default , Just a few ways , How do you give this class , Add a few more methods ?
Route::resource Before , Define the required route
Route::get('photos/popular', '[email protected]');
Route::resource('photos', 'PhotosController');
14、 What is an implicit controller ?
Route::controller('users', 'UserController');
class :
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
/**
* Response GET /users Request
*/
public function getIndex()
{
//
}
/**
* Response GET /users/show/1 Request
*/
public function getShow($id)
{
//
}
/**
* Response GET /users/admin-profile Request
*/
public function getAdminProfile()
{
//
}
/**
* Response POST /users/profile Request
*/
public function postProfile()
{
//
}
}
The above example ,index Method , Respond to the root processed by the controller URI, In this case, yes users
15、 How to name some routes in the controller ?
stay Route::controller() In the method , Pass in an array , As the third parameter
Route::controller('users', 'UserController', [
'getShow' => 'user.show',
]);
16、 Constructor dependency injection
example :
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
/**
* user Repository example .
*/
protected $users;
/**
* Create a new controller instance .
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}
17、 Methods to inject
Route::put('user/{id}', '[email protected]');
You want to extract from the controller method , Get route parameters , Then only after the dependency , Just list the routing parameters .
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Update the specified user .
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
}
边栏推荐
猜你喜欢

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

"How to carry out industrial positioning" in local / Park industrial planning

1280_ C language to find the average value of two unsigned integer

BSC parsing input data of transaction

【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。

文本关键词提取:ansj
![[team learning] SQL programming language notes - task04](/img/3e/c75f5b4610c6b6700567fe75cd2339.png)
[team learning] SQL programming language notes - task04

Development of trading system (VIII) -- Construction of low delay network

Error 1062 is reported during MySQL insertion, but I do not have this field.

1、项目第二阶段——用户注册和登陆
随机推荐
Is opencv open source?
Development of trading system (XI) -- Introduction to quickfix
地方/園區產業規劃之 “ 如何進行產業定比特 ”
Siddhartha: the book of life can be regurgitated frequently
IntStream API介绍
[harmony OS] [ark UI] basic ETS context operations
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
cesium 图形标注圆形、正方形、多边形、椭圆等
Numpy NP tips: use OpenCV to interpolate and zoom the array to a fixed shape cv2 resize(res, dsize=(64, 64), interpolation=cv2. INTER_ CUBIC)
数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
acmStreamOpen返回值问题
【LeetCode】143. Rearrange linked list
Hello CTP (V) -- CTP position calculation
虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
"Comment positionner l'industrie" dans la planification industrielle locale / parc
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
SQL, CTE, flg case problems
5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate
代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺
2022-06-21-flink-49 (I. SQL manual)