Reputation: 2193
I have installed Laravel 5.4 latest vesion in my Windows 7 64Bit, Uwamp local server.
I have already created following files in my "Custom" package that I am trying to register in Laravel:
packages/custom/timeshow/composer.json:
{
"name": "custom/timeshow",
"description": "Show Time in All Timezones",
"type": "composer-plugin",
"license": "GPL",
"authors": [
{
"name": "Test Author",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Custom\\Timeshow\\": "packages/custom/timeshow/src"
}
}
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php -r \"copy('.env.example', '.env');\"",
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}
packages\custom\timeshow\src\TimeshowServiceProvider.php
<?php
namespace Custom\Timeshow;
use Illuminate\Support\ServiceProvider;
class TimeshowServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
include __DIR__.'/routes.php';
$this->app->make('Custom\Timeshow\TimeshowController');
}
}
packages\custom\timeshow\src\TimeshowController.php
<?php
namespace Custom\Timeshow;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
class TimeshowController extends Controller
{
public function index($timezone)
{
echo Carbon::now($timezone)->toDateTimeString();
}
}
packages\custom\timeshow\src\routes.php
<?php
Route::get('timeshow/{timezone}', 'Custom\Timeshow\TimeshowController@index');
Also included my service-provider in config/app.php like below:
Custom\Timeshow\TimeshowServiceProvider::class,
But even after all this, when I run the command php artisan serve
, I get the below error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Custom\Timeshow\TimeshowServiceProvider' not found
Can someone point out what is wrong in this and assit in resolving it ?
Upvotes: 1
Views: 9436
Reputation:
I don't think you can call app->make() on your provider just yet. Register it first in the AppServiceProvier's register() method like:
$this->app->register(Custom\Timeshow\TimeshowServiceProvider::class);
After that you should be able to resolve it out of the application container. Alternatively you can call:
$this->app->singleton(Custom\Timeshow\TimeshowServiceProvider::class, function (Application $app) {
return new TimeshowServiceProvider();
});
within your package service provider. I typically bind singleton instances in the packages I write.
edit
Another thought, add your package namespace to your application composer.json as well:
"psr-4": {
"Custom\\Timeshow\\": "packages/custom/timeshow/src"
}
Upvotes: 2
Reputation: 6223
after adding custom package to composer you should run below command
composer dump-autoload
and
php artisan optimize
Upvotes: 1