Laravel 每个 url 中的参数
Laravel Parameter in every url
我几乎阅读了网络和文档中的所有内容,但找不到解决问题的方法。
我有一个变量存储在 Session
中,然后我想把这个变量放在 route('some-route')
生成的每个 url 中。
在 Session
我有 sub = "mysubid"
当我生成路由 route('my-route')
时,我想在查询字符串中传递这个 sub
参数:http://domain.dom/my-route-parameter?sub=mysubid
你能帮我解决这个问题吗?任何有用的答案将不胜感激;
您可以使用默认值功能。
首先创建一个新的中间件php artisan make:middleware SetSubIdFromSession
。然后执行以下操作:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetSubIdFromSession
{
public function handle($request, Closure $next)
{
URL::defaults(['sub' => \Session::get('sub')]);
return $next($request);
}
}
最后在 app/Http/Kernel.php
中注册您的新中间件,方法是将其添加到 $routeMiddleware
。
protected $routeMiddleware = [
// other Middlewares
'sessionDefaultValue' => App\Http\Middleware\SetSubIdFromSession::class,
];
将 {sub}
和中间件添加到您的路由定义中:
Route::get('/{sub}/path', function () {
//
})
->name('my-route')
->middleware('sessionDefaultValue');
由于您希望在每个 Web 路由上都使用它,因此您还可以将中间件添加到 web
中间件组:
protected $middlewareGroups = [
'web' => [
// other Middlewares
'sessionDefaultValue',
],
'api' => [
//
]
];
试试这个,你需要创建中间件 php artisan make:middleware SetSubSession
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetSubsSession
{
public function handle($request, Closure $next)
{
if(session('sub')){
$url = url()->full();
return redirect($url.'?sub='.session('sub'));
}
return $next($request);
}
}
在 app/http/Kernel.php
protected $routeMiddleware = [
........
'setsubsession' => \App\Http\Middleware\SetSubsSession::class,
]
在route.php中添加
Route::group(['middleware' => 'setsubsession'], function(){
//and define all the route you want to add sub parameter
});
使用这个你不需要改变你所有的 routes.This 将自动添加 "sub" 在路由中定义的中间件。
我几乎阅读了网络和文档中的所有内容,但找不到解决问题的方法。
我有一个变量存储在 Session
中,然后我想把这个变量放在 route('some-route')
生成的每个 url 中。
在 Session
我有 sub = "mysubid"
当我生成路由 route('my-route')
时,我想在查询字符串中传递这个 sub
参数:http://domain.dom/my-route-parameter?sub=mysubid
你能帮我解决这个问题吗?任何有用的答案将不胜感激;
您可以使用默认值功能。
首先创建一个新的中间件php artisan make:middleware SetSubIdFromSession
。然后执行以下操作:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetSubIdFromSession
{
public function handle($request, Closure $next)
{
URL::defaults(['sub' => \Session::get('sub')]);
return $next($request);
}
}
最后在 app/Http/Kernel.php
中注册您的新中间件,方法是将其添加到 $routeMiddleware
。
protected $routeMiddleware = [
// other Middlewares
'sessionDefaultValue' => App\Http\Middleware\SetSubIdFromSession::class,
];
将 {sub}
和中间件添加到您的路由定义中:
Route::get('/{sub}/path', function () {
//
})
->name('my-route')
->middleware('sessionDefaultValue');
由于您希望在每个 Web 路由上都使用它,因此您还可以将中间件添加到 web
中间件组:
protected $middlewareGroups = [
'web' => [
// other Middlewares
'sessionDefaultValue',
],
'api' => [
//
]
];
试试这个,你需要创建中间件 php artisan make:middleware SetSubSession
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetSubsSession
{
public function handle($request, Closure $next)
{
if(session('sub')){
$url = url()->full();
return redirect($url.'?sub='.session('sub'));
}
return $next($request);
}
}
在 app/http/Kernel.php
protected $routeMiddleware = [
........
'setsubsession' => \App\Http\Middleware\SetSubsSession::class,
]
在route.php中添加
Route::group(['middleware' => 'setsubsession'], function(){
//and define all the route you want to add sub parameter
});
使用这个你不需要改变你所有的 routes.This 将自动添加 "sub" 在路由中定义的中间件。