Configurations and Namespaces in Package Development for Laravel
Background
A few days ago, I was pulling my hair out simply because I couldn't get my configuration data. Creating packages in Laravel 4 is not as hard as it may initially seem, especially if you follow the documentation properly. I didn't, so I basically wasted around a couple of hours thinking of what I did wrong, which ended with me slacking in front of the TV.
Anyhow.
I read the documentation again, but still didn't solve my problem. Below, I will share how I was able to get my configuration data. Note that this tip will mainly cover notes not found in Laravel's documentation. Hence, do not continue reading unless you've read the documentation or done packages.
Creating Our Package
When making packages intended for Laravel, you'd execute something like php artisan workbench Zejesago/Laravel-Social --resources
. Neat. This creates some initial package structure for you in your workbench
folder.
From here on, I will refer to paths from the package directory, workbench/zejesago/laravel-social/
.
After that, you can start coding away and including configurations. To load in configuration data from config/facebook.php
, you can use \Config::get('laravel-social::facebook.appId');
. Normally, this would just work as it should, but it didn't in my case.
The Culprit
I was trying to be witty, so I changed my namespace from Zejesago\LaravelSocial\
to Zejesago\Laravel\Social\
; I just like to organize it that way. Seemingly, there shouldn't be any problem with this approach. However, I also ended up moving my service provider from Zejesago\LaravelSocial\SocialServiceProvider
to Zejesago\Laravel\Social\SocialServiceProvider
.
Solution
I found out that Config
class could not find any configuration files from the package's path, because the parent ServiceProvider
class guesses the path from the location of the service provider. In my case, SocialServiceProvider
. All that I had to do was to explicitly state the path of the package in the boot()
function.
public function boot()
{
$this->package(
'zejesago/laravel-social', null, __DIR__.'/../../..');
}
This took me a long while to finally figure it out. The fact that the Config
class is just a facade makes it even more difficult to dissect how Laravel goes about it thing. I guess I just started at the wrong end by initially looking at Config
instead of the parent ServiceProvider
class first.
Laravel Social (now called, Laravel Facebook) is a package that I created for practice with Laravel and use with projects.
Written by Zennon Gosalvez
Related protips
4 Responses
Thank you , solved my problem. No other articles , tutorials, even docs speaks about this ..
Had the exact same problem and was pulling my hair out too, thanks for sharing!
Thanks for sharing, spent an hour searching before finding your article
Hi there, in my case I was declaring
$this->package('Softpampa/package');
and not going on, but i have to declare in lower case
$this->package('softpampa/package');
now its work fine