新 Class 名称 "Song" 扩展 Laravel 5 未找到模型 Class
New Class Name "Song" Extends Laravel 5 Model Class not found
我创建了一个名为 "song" 的新 Class。
class app/ClassName 的位置,因此它是 app/songs.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
//put your code here
}
我的路线:
Route::get('/','SongsController@index');
我的 SongsController.php 位置:app/Http/controller/SongsController.php
想要对应我的数据库 table 称为 "songs"。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Song;
class SongsController extends Controller
{
public function index()
{
$songs = Song::get();
return view('pages.songsList',compact('songs'));
}
现在有了所有这些代码,我得到一个简单的错误,如下所示,
FatalErrorException in SongsController.php line 19:
Class 'App\Song' not found
如果我不添加 class "Song" 模型就可以正常工作。代码如下
DB::table('songs)->get();
提前致谢,对不起,我是 Lavaral 5 的新手。
试试这个命令:
composer dump-autoload -o
编辑: 此命令使用新的 类 更新自动加载器。
更多信息:https://getcomposer.org/doc/03-cli.md#dump-autoload
您的 class 名为 Song
,文件名为 songs.php
。您需要将文件命名为 Song.php
才能找到它。
Composer 生成一个 PSR-4 兼容的自动加载器。
您会注意到在您的 composer.json 文件中有这样的部分
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
这告诉自动加载器 App
命名空间中的任何内容都应该在 app
目录中找到。
当调用 classes 时,任何已注册的自动加载器都会尝试加载 class 文件。这意味着 App\Song
会查找 app/Song.php
,除非您另有指定。
来自规范
When loading a file that corresponds to a fully qualified class name ...
A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a "namespace prefix") corresponds to at least one "base directory".
The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
我创建了一个名为 "song" 的新 Class。
class app/ClassName 的位置,因此它是 app/songs.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
//put your code here
}
我的路线:
Route::get('/','SongsController@index');
我的 SongsController.php 位置:app/Http/controller/SongsController.php
想要对应我的数据库 table 称为 "songs"。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Song;
class SongsController extends Controller
{
public function index()
{
$songs = Song::get();
return view('pages.songsList',compact('songs'));
}
现在有了所有这些代码,我得到一个简单的错误,如下所示,
FatalErrorException in SongsController.php line 19:
Class 'App\Song' not found
如果我不添加 class "Song" 模型就可以正常工作。代码如下
DB::table('songs)->get();
提前致谢,对不起,我是 Lavaral 5 的新手。
试试这个命令:
composer dump-autoload -o
编辑: 此命令使用新的 类 更新自动加载器。 更多信息:https://getcomposer.org/doc/03-cli.md#dump-autoload
您的 class 名为 Song
,文件名为 songs.php
。您需要将文件命名为 Song.php
才能找到它。
Composer 生成一个 PSR-4 兼容的自动加载器。
您会注意到在您的 composer.json 文件中有这样的部分
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
这告诉自动加载器 App
命名空间中的任何内容都应该在 app
目录中找到。
当调用 classes 时,任何已注册的自动加载器都会尝试加载 class 文件。这意味着 App\Song
会查找 app/Song.php
,除非您另有指定。
来自规范
When loading a file that corresponds to a fully qualified class name ...
A contiguous series of one or more leading namespace and sub-namespace names, not including the leading namespace separator, in the fully qualified class name (a "namespace prefix") corresponds to at least one "base directory".
The contiguous sub-namespace names after the "namespace prefix" correspond to a subdirectory within a "base directory", in which the namespace separators represent directory separators. The subdirectory name MUST match the case of the sub-namespace names.
The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.