【问题标题】:is it possible to overriding a parameter within back() function是否可以覆盖 back() 函数中的参数
【发布时间】:2020-01-28 09:06:08
【问题描述】:

我有一个简单的问题。

这是我的代码

  1. 路线
Route::post('change-language', 'LanguageController@changeLanguage')->name('changeLanguage')->middleware('localization');
  1. 本地化中间件
public function handle($request, Closure $next)
    {
        if (session()->has('locale') && \App\Language::get('lang')->pluck('lang')->contains(Route::getFacadeRoot()->current()->parameter('locale'))) {
            $lang = Route::getFacadeRoot()->current()->parameter('locale');
            App::setLocale($lang);
            session()->put('locale', $lang);
            return $next($request);
        }elseif(session()->has('locale') && !\App\Language::get('lang')->pluck('lang')->contains(Route::getFacadeRoot()->current()->parameter('locale'))){
            $lang = 'id';
            App::setLocale($lang);
            session()->put('locale', $lang);
            return $next($request);
        }
    }
  1. 更改语言功能
public function changeLanguage(Request $req)
    {
        return redirect()->back();
    }

正如您在本地化中间件中看到的,我根据路由 url 更改语言。当我更改 changeLocalization 的返回值时,return redirect($req->lang) 就像一个魅力。但我想重定向回以前的路线。是否可以在 back() 函数中添加或覆盖路由参数?

【问题讨论】:

  • 只需调用return back();,它将直接指向会话中的预期网址
  • 你可以使用back()而不是return redirect()->route('your route name to which you want to redirect back')
  • 谢谢回复,返回上一条路线,但不是改语言
  • 或者你也可以使用return redirect()->back()->with(your parameter you want to pass)
  • 你是这个意思吗? return redirect()->back()->with($req->lang);

标签: php laravel routes localization


【解决方案1】:

changeLanguage 函数应该负责实际更改语言,而不仅仅是重定向

这是我如何做的一个例子

routes/web.php

Route::get('lang/{locale}', 'LocaleController@update')
    ->name('locale')
    ->where('locale', '(en|fr|ar)');
// Further filter in route regex (accepts only English, French and Arabic)

LocaleController.php

<?php

namespace Caddy\Http\Controllers;

class LocaleController extends Controller
{
    public function update($locale)
    {
        // Check if the $locale passed is in the array config/app.php => locales
        if (in_array($locale, config('app.locales'))) {
            // Put the $locale in session with the same name as key
            session(['locale' => $locale]);
        }
        // redirect back
        return back();
    }
}

Locale.php 中间件

<?php

namespace Caddy\Http\Middleware;

use Closure;

class Locale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $session_locale = session('locale');
        if (in_array($session_locale, config('app.locales'))) {
            // Keep the locale if already in session
            $locale = $session_locale;
        } else {
            // Fallback to English if not defined
            $locale = config('app.locale');
        }
        app()->setLocale($locale);
        return $next($request);
    }
}

全局应用中间件

Http\Kernel.php

protected $middlewareGroups = [
        'web' => [
            \Caddy\Http\Middleware\Locale::class,
        ],

这是我的config/app.php

'locale' => 'en',
'locales' => ['en', 'fr', 'ar'],

测试

  • 转到 domain.tld/about 页面为英文
  • 将 URL 更改为 domain.tld/lang/fr 您被重定向回 /about 页面是法语的

希望对你有帮助

【讨论】:

  • 哇,我认为它会起作用,但是当 locales 来自数据库时呢?
  • 这是一个糟糕的设计决策,您将对每个请求进行查询,为什么需要数据库中的语言环境?
  • 因为管理员可以添加语言,而且我的更改语言是一个选择选项,所以我只是 foreach 它。问题是,我只想保持更简单(我猜)
  • 假设我是一名管理员,我添加了一种名为non-ex 的语言,您将如何使用它来本地化内容? non-ex 不是有效的语言环境
  • 但是为什么世界管理员想要这样做呢?但是,也许如果在 laravel 中验证了有效的语言环境,它应该可以解决问题
猜你喜欢
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 2012-07-01
相关资源
最近更新 更多