David David - 2 years ago 60
PHP Question

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><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>

and here is my 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.

But when i click on it, it redirect to a view belong to
which is show() in my Postcontroller.

I don't know why and how to make it to the right url.

Can somebody help me in this one please?

Thank you,

Answer Source

First of all, your link links to /posts/management, not /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.

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.

And you should control what is accepted as a valid postID using Route Parameters: Regular Expression Constraints.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download