RouteCollection.php 第 161 行中的 NotFoundHttpException:错误

NotFoundHttpException in RouteCollection.php line 161: error

我得到 NotFoundHttpException in RouteCollection.php line 161: 但我找不到错误。我正在使用 Laravel 5.4。

我 运行 php artisan route:list 命令,我看到了定义的(命名的)路由。

这是路由文件。

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'LinkController@create');

Route::post('/create', 'LinkController@store');

Route::get('show/{id}', 'LinkController@show')->name('show');

这是资源控制器的部分。

public function store(Request $request)
{
    $this->validate($request, [
        'url' => 'required|url'
    ]);

    // Generate string length of 6 characters
    $newHash = Str::random(6);

    // creates a $link object
    $link = new Link;

    //checks if link already exists in the database
    $link_in_db = DB::table('links')->where('url', '=', $request->url)->get();

    if($link_in_db === null){
        // sets the $link variables
        $link->url = $request->url;
        $link->hash = $newHash;

        // $link is saved in the database
        $link->save();

        // redirects to the route
        return redirect()->route('show', $link->id);
    }else{ // link is in the database
        // print_r($link_in_db); // testing purposes
        return redirect()->route('show', $link->id);
    }
}

我非常感谢任何建议。如果还有其他的数据重定向方法,请提出。

谢谢!

show 路线应该是 GET:

Route::get('show/{id}', 'LinkController@show')->name('show');

如果您在路线上定义变量,您必须提供它,除非您将其设为可选。

Route::get('show/{id?}', 'LinkController@show');

将使 id 段成为可选的,并将 /show 路由到 id 中具有空值的方法。

基本上您没有为 /show 设置路线 您为 /show/somethinghere

设置了路线