简体   繁体   English

每个 url 中的 Laravel 参数

[英]Laravel Parameter in every url

I have read almost everything in web and documentation but i can't find solution for my Problem.我已经阅读了网络和文档中的几乎所有内容,但我找不到我的问题的解决方案。

I have a variable stored in Session , then I want to put this variable in every url generated by route('some-route') .我在Session存储了一个变量,然后我想将此变量放在route('some-route')生成的每个 url 中。

In Session I have sub = "mysubid"Session我有sub = "mysubid"

When I generate Route route('my-route') I want to pass this sub parameter in query string: http://domain.dom/my-route-parameter?sub=mysubid当我生成 Route route('my-route')我想在查询字符串中传递这个sub参数: http://domain.dom/my-route-parameter?sub=mysubid

Can you help me to solve This problem?你能帮我解决这个问题吗? Any helpful answer will be appreciated;任何有用的答案将不胜感激;

You can use the Default Values feature.您可以使用默认值功能。

First create a new middleware php artisan make:middleware SetSubIdFromSession .首先创建一个新的中间件php artisan make:middleware SetSubIdFromSession Then do the following:然后执行以下操作:

<?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);
    } 
}

At the end register your new middleware in app/Http/Kernel.php by adding it to $routeMiddleware .最后在app/Http/Kernel.php注册你的新中间件,将它添加到$routeMiddleware

protected $routeMiddleware = [
   // other Middlewares
   'sessionDefaultValue' => App\Http\Middleware\SetSubIdFromSession::class,
];

Add {sub} and the middleware to your route definition:{sub}和中间件添加到您的路由定义中:

Route::get('/{sub}/path', function () {  
   //
})
->name('my-route')
->middleware('sessionDefaultValue');

Since you want this on every web route you can also add the middleware to the web middleware group:由于您希望在每个 web 路由上使用它,您还可以将中间件添加到web中间件组:

protected $middlewareGroups = [
    'web' => [
        // other Middlewares
        'sessionDefaultValue',
    ],

    'api' => [
        //
    ] 
];

Try this , You need to create middleware php artisan make:middleware SetSubSession试试这个,你需要创建中间件 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);
} 
} 

in app/http/Kernel.php在 app/http/Kernel.php

 protected $routeMiddleware = [
 ........
 'setsubsession' => \App\Http\Middleware\SetSubsSession::class,
 ]

in route.php add在 route.php 添加

 Route::group(['middleware' => 'setsubsession'], function(){ 
      //and define all the route you want to add sub parameter
 });

using this you don't need to change all your routes.This will automatic add "sub" in the route define in that middleware.使用它,您不需要更改所有路由。这将自动在该中间件中定义的路由中添加“子”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM