我正在创建 laravel 5.2 包,以下是我的文件
包/
-Shreeji/
--环/
--- Composer .json
---来源/
----环.php
----RingModel.php
----RingServiceProvider
Composer .json
{
"name": "shreeji/ring",
"description": "Simple",
"license": "MIT",
"authors": [
{
"name": "author",
"email": "email@gmail.com"
}
],
"autoload": {
"psr-4": {
"Shreeji\\Ring\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
"Illuminate/support": "~5"
}
}
环.php
namespace Shreeji\Ring;
use Illuminate\Http\Response;
Class Ring {
private $ringmodel;
protected $table_name = null;
function __construct() {
}
function set_table($table_name)
{
$this->table_name = $table_name;
$this->ringmodel = New RingModel($this->table_name);
return $this;
}
}
环形模型.php
use \Illuminate\Database\Eloquent\Model as Eloquent;
class RingModel extends Eloquent {
// Set table name;
protected $table;
protected $primary_key;
public function __construct($table)
{
$this->table = $table;
}
}
RingServiceProvider.php
namespace Shreeji\Ring;
use Illuminate\Support\ServiceProvider;
Class RingServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('ring', function($app){
return new Ring;
});
}
public function boot()
{
}
}
在 app/Http/Controllers 中我创建了这样的测试文件
RingController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Shreeji\Ring;
class RingController extends Controller
{
public function index()
{
$ring = New Ring();
$ring->set_table('ring');
}
}
在 Routes.php 中
Route::get('ringtest', [ 'as' => 'ringtest', 'uses' => 'RingController@index' ]);
我在 config/app.php 中添加了服务提供者
Shreeji\Ring\RingServiceProvider::class,
在 composer.json 中,我将其添加为
.....
"psr-4": {
"App\\": "app/",
"Shreeji\\Ring\\": "packages/Shreeji/Ring/src"
}
.....
当我从浏览器调用 ringtest 时,出现以下错误。
RingController.php 第 19 行中的 FatalErrorException:找不到类 'Shreeji\Ring'
我的代码有什么问题为什么找不到这个类我还运行了 composer dumpautoload。
最佳答案
在你的 Controller 中你有:
use Shreeji\Ring;
但是,它必须是:
use Shreeji\Ring\Ring;
第一个“环”是目录(命名空间)。第二个“环”是类(class)。
您的模型不在您的命名空间中。模型的第一行必须是:
namespace Shreeji\Ring;
关于php - 找不到 Laravel 5 自定义包类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34827890/