zoukankan      html  css  js  c++  java
  • laravel路由无法访问,报404,No query results for model [AppModels...]

    今天遇到了一个问题,在routes/web.php中配置了路由,但始终无法访问该路由,一直报404。

    Route::resource('gift_packs', 'GiftPacksController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
    Route::get('gift_packs/test', 'GiftPacksController@test')->name('gift_packs.test');
    

    然后我在app/Exceptions/Handler.php文件中,修改render()方法:

    public function render($request, Exception $exception)
    {
        dd($exception);
        return parent::render($request, $exception);
    }
    

    把异常打印出来:

    No query results for model [AppModelsGiftPack].
    

    先通过 php artisan route:list 查看路由列表

    | Domain | Method                                 | URI                                                   | Name                            | 
    |        | GET|HEAD                               | gift_packs/{gift_pack}                                | gift_packs.show                 | 
    |        | DELETE                                 | gift_packs/{gift_pack}                                | gift_packs.destroy              | 
    |        | PUT|PATCH                              | gift_packs/{gift_pack}                                | gift_packs.update               | 
    |        | GET|HEAD                               | gift_packs/{gift_pack}/edit                           | gift_packs.edit                 | 
    |        | GET|HEAD                               | gift_packs/test                                       | gift_packs.test                 | 
    

    原因是laravel路由访问检测是从上到下的。

    针对同路径,gift_packs/{gift_pack} 和 gift_packs/test,当我们访问 /gift_packs/test时,路由 gift_packs/{gift_pack} 已经解析了。

    字符串 'test' 无法获取GiftPack模型数据,所以就报错了。

    解决方法是修改路由配置的上下位置:

    Route::get('gift_packs/test', 'GiftPacksController@test')->name('gift_packs.test');
    Route::resource('gift_packs', 'GiftPacksController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);
    

    这样就可以了。

      

  • 相关阅读:
    百分比布局中的居中
    struts2常用标签详解
    Struts2常用标签总结
    Struts2中action接收参数的三种方法及ModelDriven跟Preparable接口结合JAVA反射机制的灵活用法
    Dbutils学习(介绍和入门)
    Ajax与JSON的一些总结
    CURD定义
    java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
    a标签设置高度不生效问题
    使用iframe标签时如何通过jquery隐藏滚动条
  • 原文地址:https://www.cnblogs.com/jkko123/p/10773000.html
Copyright © 2011-2022 走看看