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.
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:
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.
Up until Laravel 7, the RouteServiceProvider.php file had the following code:
直到Laravel 7,RouteServiceProvider.php文件具有以下代码:
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:
In Laravel 8, the $namespace variable was removed and the Route declaration was changed to:
在Laravel 8中,删除了$ namespace变量,并且Route声明更改为:
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.
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.
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:
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.
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.
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:
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.
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.
<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('...
<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>...
<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> ...
<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 ...
// vendor/laravel/framework/src/Illuminate/Session/Store.php // 添加下面方法,然后在需要的地方,Session::hasSession() public function hasSession(){ return $this->handler->read($this...