当前位置:网站首页>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)
{
//
}
}
边栏推荐
- Cesium loading display thermal diagram
- 学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
- 数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
- numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
- [harmony OS] [arkui] ETS development graphics and animation drawing
- 虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
- openmmlab-环境配置
- How to use crawlers to capture bullet screen and comment data of station B?
- NFT insider 63: the sandbox reached a cooperation with Time magazine, and YGG established Spain's subdao
- 冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
猜你喜欢

"Renaissance" in the digital age? The bottom digital collection makes people happy and sad

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

冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市

Lecture record: data processing methods and applications of various spatial geodetic techniques

Cesium drag 3D model

讲座记录《多种空间大地测量技术的数据处理方法和应用》

Crawler crawls Sina Weibo data

论文阅读《LSD-SLAM: Large-Scale Direct Monocular SLAM》

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

Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.
随机推荐
UCLA | 用于黑盒优化的生成式预训练
文本关键词提取:ansj
1280_C语言求两个无符号整形的平均值
Lecture record: data processing methods and applications of various spatial geodetic techniques
【LeetCode】22. 括号生成
@Requestbody solution get parameter is null
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu
95% 程序员都在这里摸鱼……
1. Phase II of the project - user registration and login
Color NFT series representing diversity launched on the sandbox market platform
[openwrt] we recommend a domestically developed version of openwrt, an introduction to istoreos. It is very easy to use. It is mainly optimized. It solves the problem of Sinicization.
numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
Development of trading system (II) -- market data
地方/园区产业规划之 “ 如何进行产业定位 ”
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
DAP数据调度功能完善说明
Development of trading system (I) -- Introduction to trading system
numpy np tips: numpy数组的squeeze等处理
Openmmlab environment configuration
opencv怎么安装?opencv下载安装教程