Home  >  Article  >  Backend Development  >  laravel 路由如何写,才能实现 /post 和 /post/ 指向一个位置?

laravel 路由如何写,才能实现 /post 和 /post/ 指向一个位置?

WBOY
WBOYOriginal
2016-06-06 20:36:371509browse

现在,/post/总会跳到别的页面,而不是跳到/post的位置。

我的laravel版本是4.2,下面是测试代码。

<code>php</code><code>Route::get('post', function() { return 'w/out slash'; });
Route::get('post/', function() { return 'with slash'; });
</code>
<code>php</code><code>Route::get('post/', function() { return 'with slash'; });
Route::get('post', function() { return 'w/out slash'; });
</code>

以上两种配置,结果都如下,都是/post/ 301 跳到别的页面,/post页面正常。

<code>curl http://localhost/gitcafe/public/post
with slash
curl http://localhost/gitcafe/public/post/


<title>301 Moved Permanently</title>

<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://localhost/post">here</a>.</p>

</code>

回复内容:

现在,/post/总会跳到别的页面,而不是跳到/post的位置。

我的laravel版本是4.2,下面是测试代码。

<code>php</code><code>Route::get('post', function() { return 'w/out slash'; });
Route::get('post/', function() { return 'with slash'; });
</code>
<code>php</code><code>Route::get('post/', function() { return 'with slash'; });
Route::get('post', function() { return 'w/out slash'; });
</code>

以上两种配置,结果都如下,都是/post/ 301 跳到别的页面,/post页面正常。

<code>curl http://localhost/gitcafe/public/post
with slash
curl http://localhost/gitcafe/public/post/


<title>301 Moved Permanently</title>

<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://localhost/post">here</a>.</p>

</code>

http://stackoverflow.com/questions/22063520/laravel-slash-after-url-re...

会吗?L5 Route组件的行为我记得是:

  1. 无论带结尾斜线与否,均视为同一个路由;
  2. 带斜线的301跳转到不带斜线的(至少对GET如此);
  3. 同一个路由指定两次时,后边的覆盖前边的。

以下是测试用例与结果:

Case 1

<code>$ diff my_l5_app/app/Http/routes.php l5_fresh/app/Http/routes.php
22,24d21
</code>

Case 2

<code>$ diff my_l5_app/app/Http/routes.php l5_fresh/app/Http/routes.php
22,24d21
</code>

因为 /post/和/post 是两个路由路径, 要让他俩指向一个位置,分配配置他俩,都指向同一个位置。

Laravel不知道,不过其他路由我都是后边加?的
/post/?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn