user6046313
user6046313

Reputation:

Laravel error: route not defined

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');
    }
}

Upvotes: 0

Views: 1671

Answers (3)

Matiur Rahman Mozumdar
Matiur Rahman Mozumdar

Reputation: 451

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');

Upvotes: 0

Brian Glaz
Brian Glaz

Reputation: 15696

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

Upvotes: 2

Andy Holmes
Andy Holmes

Reputation: 8087

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

Upvotes: 1

Related Questions