【发布时间】:2018-10-05 15:09:18
【问题描述】:
仅用于实验目的。我有一个自制的 index.php,我有一个名为 Something.php 的 Laravel 模型(使用 php artisan make:model Something 制作)。
我如何将 Something.php 加载到 index.php 并拥有所有功能,例如使用 Laravel 加载模型?
目前我有这些代码:
<?php
# This is just for testing if Laravel model can be loaded independently
require_once('../vendor/autoload.php');
require_once('../bootstrap/app.php');
require_once('OtherPackage.php');
require_once('Testimonial.php');
use Test\Sample;
use App\Testimonial;
echo 'Hello World<br />';
$s = new Sample();
$t = Testimonial::all();
echo print_r($s, true);
echo '<br />';
echo print_r($t, true);
使用php -S localhost:8080会出现这个错误:
[Wed Apr 25 15:02:50 2018] ::1:58542 [500]: / - Uncaught Error: Call to a member function connection() on null in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1135
Stack trace:
#0 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1101): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(931): Illuminate\Database\Eloquent\Model->getConnection()
#2 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(877): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testi in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 1135
我尝试使用$app->withEloquent(),正如PHP Lumen Call to a member function connection() on null 中所说的那样。但是,它返回新的错误:
[Wed Apr 25 15:10:08 2018] ::1:58700 [500]: / - Uncaught Error: Call to undefined method Illuminate\Foundation\Application::withEloquent() in /home/notalentgeek/notalentgeek/Temporary Working Folder Here/testing-testimonial-2-without-auths/app/index.php:10
我的目标是我想用 Laravel 重构旧代码。所以,我想知道用我的旧代码覆盖 Laravel 项目的 ON TOP 来“启动” Laravel 的最佳方式是什么。
【问题讨论】:
标签: php laravel refactoring