我目前正在尝试开发自定义的Laravel软件包,但是入门时遇到了一些问题。
这是我的文件结构:
-packages
- oliverbusk
-invoiceconverter
-src
-controllers
- InvoiceconverterController.php
-resources
- views
- home.blade.php
-routes
- web.php
- InvoiceConverterServiceProvider.php
如您所见,我的文件放在src/
文件夹中。
首先,这是我的composer.json
,我的包裹文件夹中:
"extra": {
"laravel": {
"providers": [
"Oliverbusk\\Invoiceconverter\\InvoiceConverterServiceProvider"
]
}
}
然后我已将其自动加载到我的项目主 composer.json
文件中:
"require": {
//....
"oliverbusk/invoiceconverter": "dev-feature-package"
},
"autoload": {
[...]
"psr-4": {
"App\\": "app/",
"Oliverbusk\\Invoiceconverter\\": "packages/oliverbusk/invoiceconverter"
}
},
这是我的服务提供商文件:
namespace Oliverbusk\Invoiceconverter;
use Illuminate\Support\ServiceProvider;
class InvoiceConverterServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//Load our routes
$this->loadRoutesFrom(__DIR__ . '/routes/web.php');
//Load our views
$this->loadViewsFrom(__DIR__ . '/resources/views', 'invoiceconverter');
}
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
}
还有我的控制器文件,位于controllers/
:
namespace Oliverbusk\Invoiceconverter\Controllers;
use App\Http\Controllers\Controller;
class InvoiceconverterController extends Controller
{
public function index()
{
return view('invoiceconverter::home');
}
}
最后,我的routes/web.php
文件:
Route::group(['namespace' => 'Oliverbusk\InvoiceConverter\Controllers'], function () {
Route::get('invoiceconverter', 'InvoiceconverterController@index');
});
Oliverbusk \ InvoiceConverter \ Controllers \ InvoiceconverterController类不存在
Composer转储自动加载显示以下内容:
Discovered Package: oliverbusk/invoiceconverter
php artisan:cache:clear
清除缓存。composer update
答案 0 :(得分:1)
您的路线中的名称空间与您实际使用的名称空间不匹配。
Route::group(['namespace' => 'Oliverbusk\InvoiceConverter\Controllers'], function () {
Route::get('invoiceconverter', 'InvoiceconverterController@index');
});
更改为
Route::group(['namespace' => 'Oliverbusk\Invoiceconverter\Controllers'], function ()
{
Route::get('invoiceconverter', 'InvoiceconverterController@index');
});
看看是否有帮助。
我还建议您将所有代码重构为大写的InvoiceConverter,因为它们是两个单独的单词。