【问题标题】:How to solve route not defined如何解决未定义的路线
【发布时间】:2020-12-12 04:55:11
【问题描述】:

每当我运行我的程序时,我都会收到一个错误“Route [userproductss.prdtview] not defined. (View: 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>

web.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
    ]);
}

【问题讨论】:

  • 使用该名称定义路由。使用php artisan route:list 列出您的路线和名称
  • @Muhammad get 不是资源控制器处理的方法名称
  • "Route [userproductss.prdtview]" ... userproductss ... 是错字吗?
  • @kerbh0lz,我已经多年没有使用 laravel 了:D,我认为是 index
  • {{ route('userproduct.prdtview',$sub_cat-&gt;id) }}更改为{{ route('userproduct.show',$sub_cat-&gt;id) }}

标签: php laravel


【解决方案1】:

错误是不言自明的,您没有为prdtview 函数定义路由。 请记住,每当您创建资源控制器并定义其路由时,都会自动创建以下带有其路由的函数。

  /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */



    public function index()
    {

    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($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)
    {
        //
    }

对于您希望路由使用的任何其他方法,您必须创建一个方法并且必须为它定义一个路由,就像您的 prdtview 函数一样,您必须明确定义一个路由。

Route::get('/prdtview/{id}','UserProductController@prdtview')->name('userproduct.prdtview);

现在,当请求到达此路由时,它将被转发到您的 prdtview 函数。

您现在可以像这样在刀片中调用您的路线

route('userproduct.prdtview', ['id' => $sub_cat->id]);

【讨论】:

    【解决方案2】:

    当你使用resource时,Laravel 会定义默认路由名称

    您可以在此处找到更多详细信息; https://laravel.com/docs/7.x/controllers#resource-controllers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2023-03-15
      • 2013-05-25
      • 2011-07-16
      • 2016-12-07
      • 2019-05-09
      • 1970-01-01
      相关资源
      最近更新 更多