Laravel路线卡在UTF-8中

时间:2019-03-01 14:05:43

标签: php laravel utf-8 routes

我让此控制器重定向:

it('should not have some text', async () => {
  await expect(element(by.id('myElemId'))).toBeVisible();
  let result = await hasText('myElemId', 'some text');
  // so if the text exists it will return true, as we don't want it to exist then we can throw our own error.
  if (result) {
    throw new Error('Should not have some text, but did.');
  }
});

这是我的路线:

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

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

当其重定向时,我会收到这样的URL-

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

我希望拥有这个URL,而不是上面的URL-

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

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

已编辑:
我有这条路线:
http://localhost:8000/estates/خانه/لندن
想要这条路线
/estates/{zone}是基本路由的子路由
但是它给我返回了一条反向路线,并且它并不友好!以及为什么我不想更改路线参数!

1 个答案:

答案 0 :(得分: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()->type传递为路由参数的理由,因为即使它为null还是无效,您也将保持相同的状态。如果您的代码中有一个新的$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变量,而不是请求参数value:

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

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