use*_*790 2 php laravel laravel-5 laravel-5.2
我正在创建laravel 5.2包,以下是我的文件
packages/
-Shreeji/
--Ring/
--- composer.json
--- src/
---- Ring.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"
}
}
Run Code Online (Sandbox Code Playgroud)
Ring.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;
}
}
Run Code Online (Sandbox Code Playgroud)
RingModel.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;
}
}
Run Code Online (Sandbox Code Playgroud)
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()
{
}
}
Run Code Online (Sandbox Code Playgroud)
在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');
}
}
Run Code Online (Sandbox Code Playgroud)
在Routes.php中
Route::get('ringtest', [ 'as' => 'ringtest', 'uses' => 'RingController@index' ]);
Run Code Online (Sandbox Code Playgroud)
我在config/app.php中添加了服务提供程序
Shreeji\Ring\RingServiceProvider::class,
Run Code Online (Sandbox Code Playgroud)
在composer.json中我添加了这个
.....
"psr-4": {
"App\\": "app/",
"Shreeji\\Ring\\": "packages/Shreeji/Ring/src"
}
.....
Run Code Online (Sandbox Code Playgroud)
当我从浏览器调用ringtest时出现以下错误.
RingController.php第19行中的FatalErrorException:找不到类'Shreeji\Ring'
我的代码有什么问题为什么没有找到这个类我也运行了composer dumpautoload.
在您的控制器中,您有:
use Shreeji\Ring;
Run Code Online (Sandbox Code Playgroud)
但是,它必须是:
use Shreeji\Ring\Ring;
Run Code Online (Sandbox Code Playgroud)
第一个'Ring'是目录(命名空间).第二个'戒指'是班级.
您的模型不在您的命名空间中.模型的第一行必须是:
namespace Shreeji\Ring;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2325 次 |
| 最近记录: |