0

Having this route in Laravel: Route::get('post/{id}/comments','PostCommentsController@showComments'); I'am trying to access it from an anchor html tag href attribute in a php view which works with angular to render a list of items. This is a piece of code from this view (_post_content.php):

    <ul class="media-list" >
       <li class="media" ng-repeat="item in items" >
       <div class="media-body">
            <div class="row">
                <div class="col-sm-9"><h5 class="media-heading">
                    <a href="post/{{item.id}}/comments">{{ item.title }}</a> </h5></div>
            </div>
      </div>
      </li>
   </ul>

The new view made by the controller PostCommentsController in the method showComments, is similar to _post_content.php but it shows comments by a post id (item.id in ng-repeat). However, for other links all over the application, even in its main layout: navbars and logo anchors, image anchors, etc; its url's are prepended by the path /post/4/comments. For example if i click in the item 4 of _post_content.php, a link called blog in the left nav bar in the main layout, shows this as url: /post/4/comments/blog Of course this route does not exists and breaks all the application.

Please, any clue to solve this strange behavior? Is it possible angular is causing it, though i'm not using angular routes?

Thanks for your help.

scniro
  • 16,844
  • 8
  • 62
  • 106

1 Answers1

0

If you are using relative paths for your other links, you should prepend them with a forward slash, so instead of:

<a href="blog">

your should have:

<a href="/blog">

That way the links will be relative to the root not to the current path (which in your case is /post/id/comments).

As an alternative you could also use the base meta tag, by including this in your pages' <head>:

<base href="http://yourdomain.com/">

But be aware that there are some side effects to using base which might affect your application. Read this for more info: Is it recommended to use the <base> html tag?.

Community
  • 1
  • 1
Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Thank you a lot @Bogdan .Both suggestions solved the issue. Sure i'll check out the special meta tag , which indeed seems very interesting. – Juan Francisco Jul 10 '14 at 21:20