I'm trying to develop a package in laravel 4 - my first attempt at a package. I found a couple of tutorials which I've tried to follow:
http://jasonlewis.me/article/laravel-4-develop-packages-using-the-workbench
and
http://culttt.com/2013/06/24/creating-a-laravel-4-package/
and of course in the official documentation.
I've followed the basic structure to create the framework. However on loading the app I get a class not found error. This relates directly to the serviceprovider I have placed in the app.php file.
here's my entry in the providers array:
'Longestdrive\Calendar\CalendarServiceProvider'
My folder structure is:
laravel/workbench/longestdrive/calendar/src/Longestdrive/Calendar
My service provider has the following entries:
<?php namespace Longestdrive\Calendar;
use Illuminate\Support\ServiceProvider;
class CalendarServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('longestdrive/calendar');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
}
I've double checked to spelling and ran a composer dump-autoload both from the root of the project and the root of the package.
I've run out of ideas for solving the class not found any ideas where I've gone wrong?
The line producing the error is this one:
C:\wamp\www\googleapi\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRepository.php
Any help appreciated
Thanks
Update: I ran a composer update as suggested in the workbench/package folder with a response nothing to update. I then ran composer at the root of the project and an error was produced:
[RuntimeException]
Error Output: PHP Fatal error: Class 'Longestdrive\Calendar\CalendarServiceProvider' not found
in C:\wamp\www\googleapi\laravel\vendor\laravel\framework\src\Illuminate\Foundation\ProviderRe
pository.php on line 123
I probably posted the wrong error line earlier. The full exception response is:
Class 'Longestdrive\Calendar\CalendarServiceProvider' not found
THe error extract:
* @param \Illuminate\Foundation\Application $app
* @param string $provider
* @return \Illuminate\Support\ServiceProvider
*/
public function createProvider(Application $app, $provider)
{
return new $provider($app);
}
which I assume relates to the service provider loader not finding the CalendarServiceProvider?
I found that running composer install from within the workbench/[vendor]/[package] folder solved the problem.
I encountered the same error, so I went deeper on its flow to knew what happens.
So dissecting a little bit basically, in the bootstrap phase, when bootstrap/autoload.php
is loaded it runs at the end:
if (is_dir($workbench = __DIR__.'/../workbench'))
{
Illuminate\Workbench\Starter::start($workbench);
}
This requires EVERY workbench/vendor/package/**/**/**/autoload.php
he found (by using Symfony Finder Component)
$finder->in($path)->files()->name('autoload.php')->depth('<= 3');
workbench/vendor/package/vendor/autoload.php
.Successively in bootstrap/start.php
it gets the 'providers' defined in config/app.php
and try to load each of them:
$providers = $config['providers'];
$app->getProviderRepository()->load($app, $providers);
and then in ProviderRepository.php
foreach ($providers as $provider)
{
$instance = $this->createProvider($app, $provider);
so we'll end up with:
public function createProvider(Application $app, $provider)
{
return new $provider($app);
where it tried to create an instance of a class isn't really autoloaded. And so that's why the exception thrown!
As @Ray said, by removing his Service from 'providers' => array(
no error is thrown cause return new $myServiceDeclaredInProviderArray($app);
never fires for that service.
As @Andrew Holt said
I found that running composer install from within the workbench/[vendor]/[package] folder solved the problem.
He's absolutely right because this create the autoload vendor dir and files, and everything works as we expect it to because it finds the autoload files:
$finder->in($path)->files()->name('autoload.php')->depth('<= 3');
php artisan dump-autoload
works as well if you remove the service from the providers array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With