0

Possible duplicate of this. But unable to get the answer in my case.

I need to use a link on my Laravel website, but it keeps resulting in "route not defined" error.

Html:

<ul class="nav navbar-nav">
@auth
    <li>
        <a href="{{ route('add-post') }}">Add post</a>
    </li>
@endauth
</ul>

web.php:

Route::get('/add-post', 'PagesController@add_post');

PagesController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function add_post()
    {
        return view('add-post');
    }
}
1
  • Any chance of feedback on the answer at all? Commented Jan 18, 2018 at 16:56

3 Answers 3

2

So you basically have two options here. When you use the route function, Laravel is looking for a named route. In order to name a route, you can add ->name('name-of-route-here') at the end of your route definition.

If you don't want to name your route, you can just use the url helper function instead of route, so your code would be url('add-post')

Documentation on named routes: https://laravel.com/docs/5.5/routing#named-routes Documentation on url function: https://laravel.com/docs/5.5/helpers#method-url

Sign up to request clarification or add additional context in comments.

Comments

1

You need to name the route in order to do that:

For example:

Route::get('/add-post', 'PagesController@add_post')->name('add-post);

This is because when you use route('add-post') are are requesting a URL based on the name set in the web.php file

2 Comments

This was the first anwser i've tried and which worked. Thanks!
@YvesSterckx no problem! Can you accept it as helpful please? (The tick icon next to the question)
0

Just change this:

<ul class="nav navbar-nav">
   @auth
     <li>
      <a href="{{ url('add-post') }}">Add post</a>
   </li>
   @endauth

web.php:

Route::get('add-post', 'PagesController@add_post');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.