1
votes

I m trying to acces the create page from the navbar when i m under the home page i can access to the url without problem ('http://todolist.test/todo/create') but when i try to acces from the show page the url have a duplication ('http://todolist.test/todo/todo/create') "todo" repeated 2 times in url .

<ul class="navbar-nav mr-auto">
          <li class="{{Request::is('/')? 'active' : ''}}">
            <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="{{Request::is('todo/create')? 'active' : ''}}">
            <a class="nav-link" href="todo/create">Create Todo</a>
          </li>


        </ul>

create page route Method :GET|HEAD | URI :todo/create | Name :todo.create | Action: App\Http\Controllers\TodosController@create |Middleware: web show page route Method:GET|HEAD | URI:todo/{todo} | Name:todo.show |Action: App\Http\Controllers\TodosController@show | Middleware:web

2
This is a common problem with relative urls(like href="todo/create") The way that I solve it is by using the asset() function like so: href="{{ asset('todo/create') }}"(assuming you are in a blade file)Rob Biermann
You can also defile it for js like so: <script>function asset(url) { return '{{ asset('') }}' + url; }</script> inside a blade fileRob Biermann
@RobBiermann thanks a lot sir it worked you are my saver but if possible can you explain me why it worked with assets and not direct link cause i want to understant things not just running code. sry for my bad english and thanks a lot againsmouk

2 Answers

0
votes

A link reference like href="todo/create" adds these words to your current url(in address bar), if the page you are on is http://yourdomain.com/ , it refers to http://yourdomain.com/todo/create

If you are then using the same code to create the link, and you click it again( assuming it is in your header or statically implemented on the page) it will direct to http://yourdomain.com/todo/create/todo/create

Therefor i strongly advice to take the dynamic approch with using a function to 'generate' your url( based on your host settings).

asset('your/extension/goes/here') is a function supplied by laravel, and it does just that: generate your base_url.

If the href="todo/create" receives a url starting with a protocol(like http and https, it wil not add the string to the url, but call it directly.

So using href="{{ asset('todo/create') }}" will render into href="http://yourdomain.com/todo/create" in every situation, thereby fixing your issue :).

1
votes

I would just add a leading / to your url:

<a class="nav-link" href="/todo/create">Create Todo</a>