简体   繁体   English

Laravel:无法路由到预期的URL

[英]Laravel: Cannot route to expected URL

Here is my nav-bar: 这是我的导航栏:

 <div class="col-md-2">
        <ul class="list-group-item">
            <li><a href="/posts"><i class="fa fa-fw fa-file</i>&nbsp;&nbsp;All Post</a>
            </li>
            <li><a href="/posts/create"><i class="fa fa-fw fa-plus-circle"></i>&nbsp;&nbsp;Create New Post</a></li>
            <li><a href="/posts/manage"><i class="fa fa-fw fa-tasks"></i>&nbsp;&nbsp;Manage Posts</a></li>
        </ul>
</div>

and here is my route.php 这是我的route.php

Route::group(['prefix' => 'posts'], function(){
Route::get('', 'PostController@index');
Route::get('create', 'PostController@create');
Route::post('confirm', 'PostController@confirmation');
Route::get('{postID}', 'PostController@show');
Route::get('posts/manage', 'PostController@manage');});

I expect when I click on the "Manage Posts" button, it will redirect me to function manage() in my PostController . 我希望当我点击“管理帖子”按钮时,它会将我重定向到我的PostController函数manage()

But when I click on it, it redirects to a view which belongs to storage/framework/views which is show() in my PostController . 但是当我点击它时,它会重定向到属于我的PostController show() storage/framework/views

I don't know why and how to make it to the right url. 我不知道为什么以及如何使它成为正确的网址。

Can somebody help me with this one please? 请问有人帮我这个吗?

Thank you. 谢谢。

First of all, your link links to /posts/management , not /posts/manage . 首先,您的链接链接到/posts/management ,而不是/posts/manage Second, you already have the prefix posts for this route-group, so the route posts/manage will be available under the url /posts/posts/manage . 其次,您已经拥有此路径组的前缀posts ,因此路径posts/manage将在url /posts/posts/manage

You also want to move the manage route before your {postID} route, because {postID} will just catch anything , so the router has to first check the manage-route, and only if it doesn't match, the catch-all route. 您还希望在{postID}路由之前移动manage路由,因为{postID}将只捕获任何内容 ,因此路由器必须首先检查管理路由,并且只有在它不匹配时,才能捕获所有路由。

And you should control what is accepted as a valid postID using Route Parameters: Regular Expression Constraints . 您应该使用postID 参数:正则表达式约束来控制接受的有效postID

Route::get('{postID}', 'PostController@show')->where('id', '[0-9]+');

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

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