Laravel 升级破坏了模型路径
我对 Laravel 项目从 v5.7 到 v9 进行了姗姗来迟的更新,并遇到了 目标类不存在
错误。我发现 本指南并使用第一种方法解决错误(将命名空间添加到 RoutesServiceProvider.php 启动函数中)。这解决了该错误,但现在,一切都给我找不到类“App\Whatever”
。
我确实注意到模型现在存储在 app
目录中的 Models
目录中,而不是直接存储在 app
中,因此已将它们移动到 >模型
。我认为这可能会破坏控制器顶部的 use App\Whatever;
行,因此我尝试了 use App\Models\Whatever
以及 使用 app\Models\Whatever
(因为目录名称中的“a”是小写的)但没有效果。
我应该注意,我并没有真正很好地掌握命名空间、MVC 框架等,所以 ELI5 等:-)
我的一些控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thing;
use App\AnotherThing;
...
public function thing_summary($id) // show the summary view of the thing
{
if(Auth::check()) {
$thing = Thing::find($id);
...
I've performed a long-overdue update on a Laravel project from v5.7 to v9 and ran into a Target class does not exist
error. I found this guide and used the first method to resolve the error (adding the namespace into the RoutesServiceProvider.php boot function). This resolved that error but now, everything is giving me Class "App\Whatever" not found
.
I did notice that models are now stored in a Models
directory within the app
directory rather than directly in app
, so have moved them to Models
. I figured that might be breaking my use App\Whatever;
lines at the top of my controllers, so I've tried use App\Models\Whatever
and also use app\Models\Whatever
(since the "a" is lowercase in the directory name) but no effect.
I should note I don't really have a good grasp of namespaces, MVC frameworks etc. so ELI5 etc :-)
Some of my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thing;
use App\AnotherThing;
...
public function thing_summary($id) // show the summary view of the thing
{
if(Auth::check()) {
$thing = Thing::find($id);
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Laravel 7/8/9 坚持严格的命名空间。当您将模型移动到新目录时,您需要更新模型本身的命名空间(如果已指定)以及任何具有该模型
use
的文件。如果模型从app/
移动到app/models
,则命名空间必须更改为App/Models
Laravel 7/8/9 sticks with strict namespacing. When you move the models to a new directory, you need to update the namespace in the models themselves (if specified at all) and any file that has a
use
for the model. If the models move fromapp/
toapp/models
, the namespace must be changed toApp/Models