我正在创建 laravel 5.2 软件包,以下是我的文件
包/
-什里吉/
--戒指/
---作曲家.json
---来源/
----戒指.php
----戒指模型.php
----RingServiceProvider
作曲家.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;
}
}
环形服务提供商.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()
{
}
}
在应用程序/Http/控制器中,我创建了这样的测试文件
环形控制器.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');
}
}
在路线中.php
Route::get('ringtest', [ 'as' => 'ringtest', 'uses' => 'RingController@index' ]);
我在配置/应用程序中添加了服务提供商.php作为
Shreeji'Ring'RingServiceProvider::class,
在 composer.json 中,我已将其添加为
.....
"psr-4": {
"App''": "app/",
"Shreeji''Ring''": "packages/Shreeji/Ring/src"
}
.....
当我从浏览器调用ringtest时,出现以下错误。
RingController 中的 FatalErrorException .php第 19 行:找不到类 'Shreeji''Ring'
我的代码有什么问题,为什么找不到这个类,我也运行了作曲家转储自动加载。
在控制器中,您有:
use Shreeji'Ring;
但是,它必须是:
use Shreeji'Ring'Ring;
第一个"环"是目录(命名空间)。第二个"环"是类。
模型不在命名空间中。模型的第一行必须是:
namespace Shreeji'Ring;
您的命名空间是正确的,除非您将 Ring 类导入 Ring 控制器
在您的环中更改控制器
use Shreeji'Ring;
自
use Shreeji'Ring'Ring;
其中第一个环是文件夹,第二个环是类。
接下来,在文件的第一行为 RingModel 类添加一个命名空间
namespace Shreeji'Ring;
并添加
use Shreeji'Ring'RingModel;
到 Ring 类以导入 RingModel 类。
然后在包的作曲家文件中更改
"Shreeji''Ring''": "src/"
自
"Shreeji''": "packages/shreeji/ring/src"
之后导航到包的目录并运行
composer dump-autoload -o
在终端或命令提示符下重新生成 composer.json 文件 。
下次添加
"Shreeji''": "packages/shreeji/ring/src"
到 autoload psr-4
Laravel 应用程序的 composer.json 文件,然后切换到 Laravel 的应用程序文件夹并运行
composer dump-autoload -o
在终端或命令提示符下重新生成 Composer.json 文件。
这是关于Laravel软件包开发的链接。
拉拉维尔包装开发