【问题标题】:Laravel route being stuck in UTF-8Laravel 路由卡在 UTF-8 中
【发布时间】:2019-03-01 14:05:43
【问题描述】:

我有这个控制器要重定向:

$zone='لندن';
$type='خانه';

return redirect()->route('searchResult',['zone' => $request->zone , 'type' => $request->type]);

这是我的路线:

Route::get('/estates/{zone}/{type}', 'EstateController@searchResult')->name('searchResult');

当它重定向时,我得到一个这样的 URL-

http://localhost:8000/estates/لندن/خانه

我想要这个网址而不是上面的-

http://localhost:8000/estates/خانه/لندن

我不想切换路由参数! 需要帮助!

已编辑:
我有这条路线:
/estates/{zone}
并希望这条路线
/estates/{zone}/{type} 成为基本路线的子路线
但它给我带来了一条反向路线,而且它并不友好!这也是我不想更改路由参数的原因!

【问题讨论】:

  • 只需在路由定义中切换{type}{zone}
  • 不能!它改变了我对另一条路线的计划!我写不想切换路由参数!
  • ['zone' => $request->zone , 'type' => $request->type] 似乎正在做它应该做的事情。为什么不在那里交换类型和区域?
  • 因为我有另一条路线 localhost:8000/estates{zone}/ 我希望这条路线:/estates/{zone}/{type} 成为基本路线的子路线!
  • 您能否再次检查您的问题并尝试以使问题更清晰的方式更新它?

标签: php laravel utf-8 routes


【解决方案1】:

我无法重现您的确切行为,但 Laravel 有一些东西可以更好地处理那些可选参数的子参数。 https://laravel.com/docs/5.7/routing#parameters-optional-parameters

定义一个路由,并在参数名称后加上?,使其成为可选:

Route::get('/estates/{zone}/{type?}', 'EstateController@searchResult')->name('searchResult');

并且在您的 Action 方法签名中,也将类型参数设为可选

<?php

public function searchResult($zone, $type=null)
{
    echo $zone.' / '.$type;

    /*if(!$type) {
        $type = 'commune';

        return redirect()->route('searchResult',['zone' => request()->zone , 'type' => $type]);
    }*/

}

在你的情况下,我真的没有理由将request()-&gt;type 作为参数传递给路由,因为即使它是否为空,你也会保持相同的状态。如果您的代码中有一个新的$type 变量,则将其传递为:

return redirect()->route('searchResult',['zone' => request()->zone , 'type' => $type]);

编辑-------

如果你在 Controller 中的代码真的是:

$zone='لندن';
$type='خانه';

return redirect()->route('searchResult',['zone' => $request->zone , 'type' => $request->type]);

那么我认为你应该使用$zone$type 变量而不是请求参数值:

$zone='لندن';
$type='خانه';

return redirect()->route('searchResult',['zone' => $zone , 'type' => $type]);

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2021-09-01
    • 2013-09-25
    • 2014-07-15
    • 2021-07-11
    • 1970-01-01
    • 2020-06-04
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多