让我首先解释一下我为达到这一点所做的工作。github上有一个叫做no-framework的教程在这里它是https://github.com/PatrickLouys/no-framework-tutorial非常好的教程!我已经完成了它,现在想添加更多库。我有自动加载的作曲家设置,文件看起来像这样。
{
"name": "xxx/no-framework",
"description": "no framework",
"authors": [
{
"name": "xxx",
"email": "xxx@gmail.com"
}
],
"require": {
"php": ">=5.5.0",
"filp/whoops": ">=1.1.2",
"patricklouys/http": ">=1.1.0",
"nikic/fast-route": "^0.7.0",
"rdlowrey/auryn": "^1.1",
"twig/twig": "~1.0",
"illuminate/database": "*"
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
在我src
的文件夹中,我创建了一个名为的文件夹Models
,在其中 一个文件夹Books.php
中,Books.php
我有这个
<?php
class Book extends \Illuminate\Database\Eloquent\Model{
protected $table = 'books';
}
在我的Bootstrap.php
文件中,我在要求作曲家自动加载器后包含了这一行
include('Database.php');
该Database.php
文件也在其中src
,看起来像这样
<?php
use \Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'test',
'password' => 'l4m3p455w0rd!',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
));
$capsule->bootEloquent();
现在是错误。当我尝试通过在我的一个控制器中尝试使用来Book
上课时use
<?php
namespace App\Controllers;
use Http\Request;
use Http\Response;
use App\Template\Renderer;
use App\Models\Book as Book;
class Pages{
private $request;
private $response;
private $renderer;
public function __construct(Request $request, Response $response, Renderer $renderer){
$this->request = $request;
$this->response = $response;
$this->renderer = $renderer;
}
public function index(){
$book = new Book;
$book->title = 'test';
$book->save();
$html = $this->renderer->render('index');
$this->response->setContent($html);
}
}
我收到一条错误消息,提示“找不到类 'App\Models\Book'”我假设我没有正确地自动加载某些东西,但是别名的东西在里面,composer.json
或者其他东西可能是错误的 idk。帮助?本教程使用了一个名为 Auryn 的依赖注入器库,也许我在那里遗漏了一些东西?我怀疑它。
编辑:如果我将use
语句更改为include
这样并像这样
在类实例化前面
include('../src/Models/Book.php');
放置一个
然后它可以工作,但这显然不是正确的方法。\
$book = new \Book;