• 2020-10-27 23:15:15
  • laravel不存在Laravel 8 was released yesterday with a ton of new features and changes. One of those changes was the removal of the default route namespacing.Laravel 8昨天发布,具有大量新功能和更改。...

    laravel 类不存在

    Laravel 8 was released yesterday with a ton of new features and changes. One of those changes was the removal of the default route namespacing.

    Laravel 8昨天发布,具有大量新功能和更改。 这些更改之一是删除了默认路由的命名空间。

    Although this change is backward-compatible, meaning that older projects that used Laravel 7.x can easily migrate to Laravel 8.x without having to change anything, new projects created in Laravel 8 (starting 8/Sep) have to take this into account.

    尽管此更改是向后兼容的,这意味着使用Laravel 7.x的较旧项目可以轻松迁移到Laravel 8.x,而无需进行任何更改,但是在Laravel 8中创建的新项目(从8月9日开始)必须考虑到这一点。

    Many developers have been facing an issue in their newly created Laravel 8 apps where they try to load their routes and they run into an Exception that says something like:

    许多开发人员在他们新创建的Laravel 8应用程序中都遇到了一个问题,他们尝试加载其路由,并且遇到了类似以下内容的Exception:

    Image for post
    Bear in mind that your error might not look exactly like this

    The issue is not that the code is broken, but that 99.9% of Laravel tutorials are now broken in this department because most of them relied on this default namespace for the string syntax.


    改变(The change)

    Up until Laravel 7, the RouteServiceProvider.php file had the following code:

    直到Laravel 7,RouteServiceProvider.php文件具有以下代码:

    Image for post
    RouteServiceProvider.php in Laravel 7.x
    Laravel 7.x中的RouteServiceProvider.php

    What this does is tell Laravel to load the routes in routes/web.php, using the web middleware and the App\Http\Controllers namespace. This, in turn, means that whenever you declared a route using the string-syntax, Laravel would look for that controller in the App\Http\Controllers folder:

    这样做是告诉Laravel使用Web中间件和App \ Http \ Controllers命名空间将路由加载到route / web.php中。 反过来,这意味着每当您使用字符串语法声明路由时,Laravel都会在App \ Http \ Controllers文件夹中查找该控制器:

    Image for post

    In Laravel 8, the $namespace variable was removed and the Route declaration was changed to:

    在Laravel 8中,删除了$ namespace变量,并且Route声明更改为:

    Image for post
    RouteServiceProvider.php in Laravel 8.x
    Laravel 8.x中的RouteServiceProvider.php

    Now, Laravel is looking for routes inside your web.php file, as always. It’s also applying the web middleware, as always. But notice that it’s no longer using the previous namespace.

    现在,Laravel一如既往地在web.php文件中寻找路由。 一如既往,它也在应用Web中间件。 但是请注意,它不再使用以前的名称空间。

    This means that starting in Laravel 8, when you declare your routes using the string-syntax, Laravel isn’t going to look for your controller inside App\Http\Controllers.

    这意味着从Laravel 8开始,当您使用字符串语法声明路由时,Laravel不会在App \ Http \ Controllers中寻找控制器。

    Image for post

    我该如何解决? (How do I fix this?)

    The problem here is that Laravel has no idea where to look for your controller, so all we have to do is let it know! There are 3 ways you can accomplish this:

    这里的问题是Laravel不知道在哪里寻找您的控制器,所以我们要做的就是让它知道! 您可以通过3种方式完成此操作:

    • Add the namespace back manually so you can use it as you did in Laravel 7.x and before

      手动重新添加名称空间,以便您可以像在Laravel 7.x及之前版本中一样使用它
    • Use the full namespace in your route files when using the string-syntax

    • Use the action syntax (recommended)


    手动添加名称空间 (Adding the namespace manually)

    This is fairly simple. Go into your RoutesServiceProvider.php file and you’ll see the following:

    这很简单。 进入RoutesServiceProvider.php文件,您将看到以下内容:

    Image for post

    All you need to do is add the following three lines to this file and Laravel will go back to using the default namespace as in Laravel 7.x:

    您需要做的就是向该文件添加以下三行,Laravel将回到使用默认名称空间的方式,如Laravel 7.x:

    Image for post

    What did we just do? We declared the $namespace variable with the default Namespace for our controllers and told laravel to use that for our web and api routes.

    我们刚刚做了什么? 我们为控制器声明了具有默认命名空间的$ namespace变量,并告诉laravel将其用于我们的Web和api路由。

    If you try to run your app again, everything should be working.


    使用完整的名称空间 (Using the full namespace)

    This one involves changing all your route declarations, but the idea is simple: prepend your controller names with their namespace. See the following example for our PostsController inside the app/Http/Controllers folder.

    这涉及到更改所有路由声明,但是想法很简单:在控制器名称前添加名称空间。 请参阅以下示例,其中包含app / Http / Controllers文件夹中的PostsController。

    Image for post

    If you try again, everything should be running smoothly.


    使用动作语法 (Using the Action Syntax)

    This is the alternative I personally recommend as I find it more typo-proof and in my experience provides better IDE support as we are explicitly telling the code which class to use. Instead of using our usual string syntax, we can use the action syntax where we specify the class and method to use in an array:

    这是我个人推荐的替代方法,因为我发现它具有更好的防错字功能,并且根据我的经验,当我们明确告诉代码要使用哪个类时,它可以提供更好的IDE支持。 除了使用通常的字符串语法,我们还可以使用动作语法,在其中指定要在数组中使用的类和方法:

    Image for post

    Notice here we are not passing the PostsController within quotes but rather PostsController::class, which internally will return ‘App\Http\Controllers\PostsController’. The second value in the array is the method to call within that controller, meaning: “In PostsController.php call the ‘all’ method.

    请注意,此处我们不是在引号中传递PostsController,而是传递PostsController :: class,该内部将返回“ App \ Http \ Controllers \ PostsController”。 数组中的第二个值是在该控制器内调用的方法,表示:“在PostsController.php中,调用'all'方法。

    Again, if you try to run your app again, everything should be up and running.


    结束语 (Closing Remarks)

    By now, your app should be up and running again. If not, please feel free to ask for help. Everyone in the community is eager to give a hand.

    现在,您的应用程序应该已重新启动并运行。 如果没有,请随时寻求帮助。 社区中的每个人都渴望伸出援助之手。

    Whether you added the namespace manually, specified the full namespace in your routes, or went with the action syntax, what you just did is tell Laravel in which namespace your controllers actually are, so now it actually knows where to look.


    If you liked what you read or want to learn more cool stuff related to Laravel, you can follow me on Twitter, where I post about coding, entrepreneurship, and living a better life.


    翻译自: https://medium.com/@litvinjuan/how-to-fix-target-class-does-not-exist-in-laravel-8-f9e28b79f8b4

    laravel 类不存在

  • 有时候您可能想要增加不存在数据库字段的属性数据。这时候只要定义一个获取器即可: public function getIsAdminAttribute() { return $this->attributes['admin']; } 定义好获取器之后,再把...


    public function getIsAdminAttribute()
        return $this->attributes['admin'];

    定义好获取器之后,再把对应的属性名称加到模型里的 appends 属性:

    protected $appends = ['is_admin'];





  • <p>I have a function where what i want to do first check order_id exists it update else insert.... <pre><code>public function possstContract(){ $postContracts = Contract::findOrNew(Input::get('...
  • laravel判断视图是否存在

    千次阅读 2018-09-20 15:03:57
    laravel有很多辅助函数,如view() 函数获取一个视图实列...exists()方法,如果存在的话返回true,不存在则返回false 以下是部分代码片段案例 $data-&gt;load(['subjects' =&gt; function ($query) { ...

    laravel有很多辅助函数,如view() 函数获取一个视图实列 



                    $data->load(['subjects' => function ($query) {
                    $tpl = 'pdf/' . strtolower(substr($data->subjects->code, 0, 1));
                    if (!view()->exists($tpl)) {
                        $tpl = 'pdf/pdf';
                    $pdf = \PDF::loadView($tpl, $data);
                    return $pdf->download('报名表.pdf');


  • 该软件包旨在为Laravel项目提供一种简便而功能强大的格式来格式化和转换货币的工具。 为什么使用moneyphp包? 因为moneyphp使用intl扩展名进行数字格式设置。 默认情况下, intl扩展在PHP安装中不存在,并且...
  • Laravel 存在SQL注入漏洞 漏洞描述: 该漏洞存在Laravel的表单验证功能,漏洞函数为ignore(),漏洞文件位于/vendor/laravel/ramework/src/Illuminate/Validation/Rules/Unique.php。有时候开发者希望在进行字段...
  • Laravel中的.env文件偶发性读取到,env()获取为空最后取了config默认的终极解决方案 1.背景 用Laravel框架开发项目,上线后打开APP运行一段时间后会莫名的自动退出登录(前端有自动轮询请求接口),经过排查...
  • 1、基于闭包的路由能被缓存。如果要使用路由缓存,你必须将所有的闭包路由转换成控制器类路由。...但是它返回请求中不存在的键值对。获取部分输入数据、如果需要获取输入数据的子集,可以使用 only
  • Laravel一、Laravel是什么?二、安装步骤1.安装PHP以及Composer2.修改Composer镜像3.Composer部署Laravel项目4.目录结构分析5.项目启动方式(推荐使用方式二)总结 一、Laravel是什么? 示例:pandas 是基于NumPy ...
  • <p>I want to validate a field only if another field is present. <p>This is my current implementation that can illustrate what I want to do....<em>I need to perform unique validation on <strong>...
  • WPCMF 开源内容管理系统 ... 当发生验证错误时,默认情况下,表单输入不会保留提交前的。这会导致糟糕的用户体验,因为用户需要重新输入信息。幸运的是,Laravel 提供了一种非常简单的方法来解决这个...如果该值不存.
  • <p>Is is possible to replace this line of code with something out of the box.... <pre><code>Cache::has('test') ? \Cache::increment('test') : \Cache::put('test', 1, 60); </code></pre> ...
  • 原因:Laravel 框架内,出于安全的原因考虑,默认对所有 cookie 进行加密存储 解决办法 找到 App\Http\Middleware\EncryptCookies 文件, 添加到排除名单 中: <?php namespace App\Http\Middleware; use ...
  • laravel 数据库获取的常用方法

    千次阅读 2019-03-05 17:25:49
    find($id) 需要一个主键$id并返回一个模型对象,若不存在则返回null findOrFail($id) 需要一个主键$id并返回一个模型对象,若不存在则发生错误,抛出一个error first() 返回数据库中找到的第一条记录,若不存在匹配...
  • 'active_url' => '是否是一个合法的url,基于PHP的checkdnsrr函数,因此也可以用来验证邮箱地址是否存在', 'after:date' => '验证字段必须是给定日期后的,比如required|date|after:tomorrow,通过PHP函数...
  • <p>I am unable to check whether following collection contains data or not <pre><code>$users = \App\Tempuser::where('mobile','=',$request->mobile)->get(); ...if(isset($users)) ...
  • <p>How to check if data exist, if it not exist then redirect to different page. <p>It keep saying data is exist even it does not exist. <p>In the model file, I have: <pre><code>public static ...
  • laravel redis操作大全

    2021-09-02 10:44:08
    普通得set/get操作,set操作,如果键名存在,则会覆盖原有得 $redis = app("redis.connection"); $redis->set('library' , 'phpredis');//存储key为library ,phpredis得记录 $redis->get("library");//...
  • // vendor/laravel/framework/src/Illuminate/Session/Store.php // 添加下面方法,然后在需要的地方,Session::hasSession() public function hasSession(){ return $this-&gt;handler-&gt;read($this...
  • 2、add() 若key2不存在,则添加成功 否则,添加失败 Cache::add('key2','val2',20); 3、forever() 永久保存对象到缓存 Cache::forever('key3','val3'); 4、has() 判断是否存在 Cache::has('key1'); 5、get() ...
  • 为每一个Model中创建一个能够自动生成... (略) 文档中说的使用接口方式,而且返回文档中的格式,是能解决自动选中原值的,只能是空白的。 $form->select('user_id','作者')->options(admin_base_path('/api/users'));
  • // 合并输入,如果有相同的key,用户输入的会被替换掉,否则追加到 input $request->merge(['foo' => 'bar', ....]); // 替换所有输入 $request->replace([..]) // 设置某参数,如果存在则替换,否则新增...
  • 但是却在laravel不太适用,经过考察下来的时候 session的类以及助手方法 是laravel框架自己创造的,基层运行 优先级应该是低于构造 导致构造拿到 记录到file 的session, 那根据这点 我们只能移植到中间件来判断~...
  • 版权声明:本文为博主原创文章...laravel validator unique 'name' => 'required|unique:test,display_name,11,age,extra_field,extraValue' 解释 对于客户端输入的 name 字段 name 能为空 n...
  • 我尝试了很多验证文档中的方法,但都给出了同样的错误说明: <p>Array to string conversion 下面是我如何添加数组的: <pre><code>$this->validate($request,[ 'employee' =>... </div>
  • 编辑您的Laravel .env文件 该软件包为您提供了通过控制器或模型动态编辑.env的可能性。 当前版本(2.x)附带基于VueJS的图形用户界面,可为您提供所有功能的非常简单的实现。 可用功能列表: 检查给定密钥是否...
  • 用户认证在任何框架内都是很重要的一部分,Laravel为用户认证提供了丰富的实现方式,包括但不仅限于本文将要解决以下问题:我们实现的token是api-token,最直观描述是,手动设置token,自动校验token并返回用户表...
  • laravel validate验证参数说明

    千次阅读 2020-10-24 19:40:15
    laravel validate验证参数说明 validate的介绍 validate的使用 validate验证参数说明
  • laravel验证规则

    2019-11-11 17:16:38
    该字段的必须是yes、on、1或true.必填规则 'is_read'=>'accepted' active_url 必须是一个基于PHP函数checkdnsrr 的有效URL, 可以认为是存在的一个域名地址 'url'=>'active_url' after:date 给定...



1 2 3 4 5 ... 20
收藏数 5,569
精华内容 2,227

laravel 值不存在