【问题标题】:Laravel 5.2 NotFoundHttpException in RouteCollection.php line 161 ErrorLaravel 5.2 NotFoundHttpException in RouteCollection.php 第 161 行错误
【发布时间】:2016-07-01 22:05:32
【问题描述】:

我正在关注 tuts+ 视频系列以开发电子商务网络应用程序。我几乎完成了它,但我在商店部分遇到了一个问题。我在/store 向最终用户的路线上显示所有产品的列表,我这样做了,但现在我要建立的是每个产品的单一视图页面。简单地说,当我在商店页面上显示所有产品时,我希望每个产品都与他自己的单页视图相关联,即价格、图片和有关该特定帖子的其他数据。我关注了视频,但是当我尝试访问产品的单个页面时,它会引发以下错误。

错误:

NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('D:\xampp\htdocs\ecom\public\index.php') in index.php line 21

存储所有产品或展示的索引文件

    @foreach($products as $product)
                        <div class="col-md-4 col-lg-4 col-sm-12">
                                <li>{!! $product->title  !!} - ({!! $product->price !!})</li>
                                <p> {!! $product->description !!}</p>
                                <a href="store/view/{!! $product->id !!}">
                                    {!! Html::image($product->image ,$product->title) !!}</p>
                                </a>        
                              {!! Form::close() !!}
                         </div>
   @endforeach

View.blade.php 或产品单页:

{!! $product->title !!} <br>
{!! $product->description !!} <br>
{!! $product->image !!}

StoreController 代码:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Category;
use App\product;
use View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class StoreController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return View::make('store.index')->with('products', product::take(4)->orderBy('created_at','DESC')->get());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return View::make('InsertCategory');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            $category = new Category;
            $category->name = Input::get('name');
            $category->save();      
            return Redirect::to('admin/categories')->with('message', 'Category Created');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return View::make('store.view')->with('product', product::find($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = Category::find(Input::get('id'));
        var_dump($input);
        if($category){
            $category->delete();
            return Redirect::to('admin/categories');
        }
    }
}

Routes.php:

Route::get('/', 'StoreController@index');
Route::get('/admin', function () {
    return view('welcome');
});
Route::resource('store', 'StoreController');
Route::resource('admin/categories', 'CategoriesController');
Route::resource('admin/products', 'ProductsController');

PS : 如果大家还需要什么,请在评论中提及,我会补充。

【问题讨论】:

  • @iam-decoder ahahaha 为什么这么严重?这是冒犯性的吗?
  • 据我观察,您在调用控制器时没有调用任何方法。

标签: php routes laravel-routing laravel-5.2


【解决方案1】:

在您的商店索引刀片中,您没有使用相对链接,因此它将 store/view/{!! $product-&gt;id !!} 附加到当前 url(没有 QS)是什么。

如果您在该 href 的开头添加 / 可能会解决您的问题。您的商店控制器中似乎也没有 view 方法,请尝试将其更改为 show

@foreach($products as $product)
    <div class="col-md-4 col-lg-4 col-sm-12">
        <li>{!! $product->title  !!} - ({!! $product->price !!})</li>
        <p> {!! $product->description !!}</p>
        <a href="/store/{!! $product->id !!}">
            {!! Html::image($product->image ,$product->title) !!}
        </a>
    </div>
@endforeach

【讨论】:

  • @LaravelWarrior 当你点击一个产品时,当前的 url 地址是什么?
  • @LaravelWarrior 你的控制器中没有view 方法,尝试将其更改为show
  • 我将 view.blade.php 更改为 show.blade.php 并在控制器中返回 store.show ,并将 URL 更改为 store/show/ {!! $product->id !!} 还是一样的错误
  • @LaravelWarrior 我假设你的新网址是localhost/ecom/store/show/12。它必须是localhost/ecom/store/12,所以实际上只需从href 中删除“show”这个词。我已经为你编辑了我的答案
  • 是的,它就像一个魅力。但是为什么你从 URL 中删除了 show ,如果你能解释一下那就太好了!
【解决方案2】:

如果你拼错了路线名称,这个异常很容易重现:

NotFoundHttpException in RouteCollection.php

好像没有这样的路线。

试试

php artisan route:list

问题可能是您拼错了路线名称。 可能是aricles 而不是articles 之类的东西。

【讨论】:

    【解决方案3】:

    这是因为您没有调用要执行的方法/函数。例如,在您的“StoreController”上,您将执行您的Index 你应该打电话给你的routes.php

    喜欢这个

    Route::resource('store', 'StoreController@index');
    

    【讨论】:

    • @Felix Kamote 尝试过,但仍然无法正常工作,顺便说一下,它是资源控制器,我们不需要指定像 controller@func bcz 这样的函数,它会自动为我们执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2016-07-03
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多