将参数传递给 Laravel 中间件
Pass parameter to Laravel Middleware
如何在中间件中传递参数?我总是收到这个错误
这是我的中间件的结构
<?php
namespace App\Http\Middleware;
use Closure;
class SubDomainAccess
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $subdomain)
{
dd($subdomain); // Just trying to output the result here
return $next($request);
}
}
并且在 Kernel.php 下 $routeMiddleware 我添加了这个
'subdomain.access' => \App\Http\Middleware\SubDomainAccess::class,
现在在我的 web.php 路由文件中添加了这个
Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing'])->middleware('subdomain.access');
});
我也试过这个
Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
Route::group(['middleware' => 'subdomain.access'], function () {
Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing']);
});
});
我试过了,但没有用。我唯一没有尝试过的是将中间件放在我的控制器构造函数中。但我不希望那样做,因为我认为这很混乱,如果它在路由文件中会更优雅。
希望你能帮我解决这个问题。谢谢
['middleware' => 'subdomain.access']
是错误的,请尝试将 ['middleware' => 'subdomain:access']
与 :
一起使用。
https://mattstauffer.co/blog/passing-parameters-to-middleware-in-laravel-5.1
好的,多亏了这个 link
,我设法找到了一种无需在中间件句柄函数上传递第三个参数即可获取参数的方法
所以我检索 subdomain 参数所做的就是这个
$request->route()->parameter('subdomain')
或者如果所有参数
$request->route()->parameters()
从 $request
对象和 return 域获取 URI。无需将子域作为参数传递给中间件。
namespace App\Http\Middleware;
use Closure;
class SubDomainAccess
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $subdomain)
{
$sudomain = $this->getSubDomain($_SERVER['HTTP_HOST']);
return $next($request);
}
/**
* Get Subdomain name
* @param $uri
* @return bool
*/
private function getSubDomain($uri)
{
if(!empty($uri))
{
$host = explode('.', $uri);
if(sizeof($host) > 2)
return $host[0];
}
return false;
}
}
如何在中间件中传递参数?我总是收到这个错误
这是我的中间件的结构
<?php
namespace App\Http\Middleware;
use Closure;
class SubDomainAccess
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $subdomain)
{
dd($subdomain); // Just trying to output the result here
return $next($request);
}
}
并且在 Kernel.php 下 $routeMiddleware 我添加了这个
'subdomain.access' => \App\Http\Middleware\SubDomainAccess::class,
现在在我的 web.php 路由文件中添加了这个
Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing'])->middleware('subdomain.access');
});
我也试过这个
Route::group(['domain' => '{subdomain}.' . config('site.domain')], function () {
Route::group(['middleware' => 'subdomain.access'], function () {
Route::get('/', ['as' => 'site.home', 'uses' => 'Site\Listing\ListingController@showListing']);
});
});
我试过了,但没有用。我唯一没有尝试过的是将中间件放在我的控制器构造函数中。但我不希望那样做,因为我认为这很混乱,如果它在路由文件中会更优雅。
希望你能帮我解决这个问题。谢谢
['middleware' => 'subdomain.access']
是错误的,请尝试将 ['middleware' => 'subdomain:access']
与 :
一起使用。
https://mattstauffer.co/blog/passing-parameters-to-middleware-in-laravel-5.1
好的,多亏了这个 link
,我设法找到了一种无需在中间件句柄函数上传递第三个参数即可获取参数的方法所以我检索 subdomain 参数所做的就是这个
$request->route()->parameter('subdomain')
或者如果所有参数
$request->route()->parameters()
从 $request
对象和 return 域获取 URI。无需将子域作为参数传递给中间件。
namespace App\Http\Middleware;
use Closure;
class SubDomainAccess
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $subdomain)
{
$sudomain = $this->getSubDomain($_SERVER['HTTP_HOST']);
return $next($request);
}
/**
* Get Subdomain name
* @param $uri
* @return bool
*/
private function getSubDomain($uri)
{
if(!empty($uri))
{
$host = explode('.', $uri);
if(sizeof($host) > 2)
return $host[0];
}
return false;
}
}