当前位置:网站首页>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
- MySQL插入过程报错1062,但是我没有该字段。
- Crawler grabs the data of Douban group
- 佐喃社区
- Hello CTP (II) -- Introduction to CTP
- acmStreamOpen返回值问题
- Shutter fittedbox component
- Development of trading system (III) - risk control system
- NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO
- 如何绘制产业招商地图
猜你喜欢

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

【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
![[harmony OS] [ark UI] basic ETS context operations](/img/fb/a1b8463ba160e6c5aa23d671a0c245.png)
[harmony OS] [ark UI] basic ETS context operations

5 key indicators of SEO: ranking + traffic + session + length of stay + bounce rate

PHP代码审计1—PHP.ini的那些事

Intel 13th generation core showed its true colors for the first time: 68mb cache improved significantly

2. play the chromatic harmonica

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

Read lsd-slam: large scale direct monolithic slam

How to draw an industry investment map
随机推荐
navicat可不可以直接操作安卓数据库SQLite
文本关键词提取:ansj
Read lsd-slam: large scale direct monolithic slam
Flutter Builder & futurebuilder components
Development of trading system (x) -- fix agreement
Simple integration of client go gin 11 delete
2. play the chromatic harmonica
Coinlist queuing tutorial to improve the winning rate
95% 程序员都在这里摸鱼……
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding
虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
client-go gin的简单整合十-Update
MySQL插入过程报错1062,但是我没有该字段。
Development of trading system (VI) -- HFT high frequency trading
冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
OBS Browser+浏览器的基本使用
【Kubernetes系列】Helm的安装使用
Hello CTP (V) -- CTP position calculation
numpy np tips: numpy数组的squeeze等处理
Lecture record: new application of inertial navigation - inertial measurement