各种 Laravel 控制器方法之间的通用逻辑
Common logic between various Laravel controllers method
假设我有这样一个 URL:
/city/nyc (display info about new york city)
还有一个像这样:
/city/nyc/streets (display a list of Street of nyc)
我可以将它们绑定到这样的方法:
Route::get('city/{city}', 'CityController@showCity');
Route::get('city/{city}/streets', 'CityController@showCityStreet');
问题是我需要在这两种方法上对城市执行一些检查(例如,如果数据库中存在 {city})。
我可以创建一个方法并像这样在两者中调用它们:
class CityController {
private function cityCommonCheck($city) {
// check
}
public function showCity($city) {
$this->cityCommonCheck($city);
// other logic
}
public function showCityStreet($city) {
$this->cityCommonCheck($city);
// other logic
}
}
有没有更好的方法?
我认为最好的方法是,您可以将通用逻辑移动到下面的 Model.So 您的代码中。
class CityController {
public function showCity($city) {
City::cityCommonCheck($city);
}
public function showCityStreet($city) {
City::cityCommonCheck($city);
}
}
型号class
class City{
public static function cityCommonCheck($city) {
//put here your logic
}
}
通过这种方式,您可以从任何控制器调用 cityCommonCheck 函数。
即使您有不同的想法,我相信中间件是最好的解决方案。
首先,使用php artisan make:middleware CityCheckMiddleware
在App/Http/Middleware
中创建一个class。然后编辑该方法以执行您的检查应该执行的操作并添加一个构造函数以注入 Router
public function __construct(\Illuminate\Http\Routing\Router $router){
$this->route = $router;
}
public function handle($request, Closure $next)
{
$city = $this->route->input('city');
// do checking
return $next($request);
}
在App/Http/Kernel.php
中定义一个shorthand键:
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
// ...
'city_checker' => 'App\Http\Middleware\CityCheckerMiddleware',
];
然后,在您的控制器中:
public function __construct()
{
$this->middleware('city_checker', ['only' => ['showCity', 'showCityStreet']]);
}
假设我有这样一个 URL:
/city/nyc (display info about new york city)
还有一个像这样:
/city/nyc/streets (display a list of Street of nyc)
我可以将它们绑定到这样的方法:
Route::get('city/{city}', 'CityController@showCity');
Route::get('city/{city}/streets', 'CityController@showCityStreet');
问题是我需要在这两种方法上对城市执行一些检查(例如,如果数据库中存在 {city})。 我可以创建一个方法并像这样在两者中调用它们:
class CityController {
private function cityCommonCheck($city) {
// check
}
public function showCity($city) {
$this->cityCommonCheck($city);
// other logic
}
public function showCityStreet($city) {
$this->cityCommonCheck($city);
// other logic
}
}
有没有更好的方法?
我认为最好的方法是,您可以将通用逻辑移动到下面的 Model.So 您的代码中。
class CityController {
public function showCity($city) {
City::cityCommonCheck($city);
}
public function showCityStreet($city) {
City::cityCommonCheck($city);
}
}
型号class
class City{
public static function cityCommonCheck($city) {
//put here your logic
}
}
通过这种方式,您可以从任何控制器调用 cityCommonCheck 函数。
即使您有不同的想法,我相信中间件是最好的解决方案。
首先,使用php artisan make:middleware CityCheckMiddleware
在App/Http/Middleware
中创建一个class。然后编辑该方法以执行您的检查应该执行的操作并添加一个构造函数以注入 Router
public function __construct(\Illuminate\Http\Routing\Router $router){
$this->route = $router;
}
public function handle($request, Closure $next)
{
$city = $this->route->input('city');
// do checking
return $next($request);
}
在App/Http/Kernel.php
中定义一个shorthand键:
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
// ...
'city_checker' => 'App\Http\Middleware\CityCheckerMiddleware',
];
然后,在您的控制器中:
public function __construct()
{
$this->middleware('city_checker', ['only' => ['showCity', 'showCityStreet']]);
}