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

时间:2017-03-30 12:34:05

标签: php laravel laravel-5.4

Screenshot

我收到NotFoundHttpException in RouteCollection.php line 161:但我无法找到错误。我使用的是Laravel 5.4。

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

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

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

谢谢!

2 个答案:

答案 0 :(得分:3)

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

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

将使id段可选,并将/show路由到id为null值的方法。

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

的路线

答案 1 :(得分:2)

GET路线应为Route::get('show/{id}', 'LinkController@show')->name('show');

{{1}}
相关问题