简体   繁体   English

Laravel错误:路由未定义

[英]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. 我需要使用Laravel网站上的链接,但该链接始终导致“路由未定义”错误。

Html: HTML:

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

web.php: web.php:

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

PagesController: PagesController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function add_post()
    {
        return view('add-post');
    }
}

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 这是因为当您使用route('add-post')它们正在根据web.php文件中设置的名称请求URL。

So you basically have two options here. 因此,这里基本上有两个选择。 When you use the route function, Laravel is looking for a named route. 使用route功能时,Laravel会查找已命名的路由。 In order to name a route, you can add ->name('name-of-route-here') at the end of your route definition. 为了命名路径,您可以在路径定义的末尾添加->name('name-of-route-here')

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') 如果您不想命名您的路线,则可以只使用url helper函数而不是route ,因此您的代码应为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 有关命名路由的文档: https : //laravel.com/docs/5.5/routing#named-routes有关url函数的文档: https : //laravel.com/docs/5.5/helpers#method-url

Just change this: 只需更改此:

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

web.php: web.php:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM