路径未定义如何解决

每当我运行程序时,都会收到错误“路线 [userproductss.prdtview] 未定义。(视图:D:\xampp\htdocs\E-commerce\resources\views\layouts\includes\top.blade.php)”。代码如下


刀刃:


<ul>

    <li class="active"><a href="">Home</a></li>

    @foreach($categories as $category)

        <li><a href="#">{{$category->cat_name}}</a>

        <ul class="dropdown">

            @foreach($subcat as $sub_cat)

            @if($sub_cat->cat_id == $category->id)

            <li><a href="{{ route('userproduct.prdtview',$sub_cat->id) }}"><?php echo $sub_cat->sub_cat_name; ?></a></li>

            @endif

            @endforeach

        </ul>

    </li>

    @endforeach                        

</ul>

网页.php


Route::resource('userproduct', 'UserProductController');

控制器:


public function prdtview($id)

{

    $data=DB::select('select category.cat_name,product_images.prdt_image,product.prdt_name,product.actual_price from product INNER JOIN category on product.catid = category.id INNER JOIN product_images on product.id = product_images.prdt_id where product.sub_cat_id = $id ');

    return view("frontend.product",[

        'data' => $data

    ]);

}


慕沐林林
浏览 124回答 2
2回答

哈士奇WWW

该错误是不言自明的,您尚未为您的函数定义路径prdtview。请记住,每当您创建资源控制器并定义其路由时,就会自动创建以下函数及其路由。&nbsp; /**&nbsp; &nbsp; &nbsp;* Display a listing of the resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Show the form for creating a new resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function create()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Store a newly created resource in storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function store(Request $request)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Display the specified resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; int&nbsp; $id&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function show($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Show the form for editing the specified resource.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; int&nbsp; $id&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function edit($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Update the specified resource in storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; int&nbsp; $id&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function update(Request $request, $id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Remove the specified resource from storage.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; int&nbsp; $id&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function destroy($id)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; }对于您希望路由使用的任何其他方法,您必须创建一个方法并且必须为其定义一个路由,就像您的prdtview函数必须显式定义一个路由一样。Route::get('/prdtview/{id}','UserProductController@prdtview')->name('userproduct.prdtview);现在,当请求到达此路由时,它将被转发到您的prdtview函数。您现在可以在 Blade 中调用您的路线,例如route('userproduct.prdtview', ['id' => $sub_cat->id]);

ibeautiful

Laravel 在使用时定义默认路由名称resource。
打开App,查看更多内容
随时随地看视频慕课网APP