I have the following dir in my laravel-4 application:
app/Latheesan
This has been added to my composer.json
like this:
"autoload": {
...
"psr-0": {
"Latheesan": "app/"
}
...
},
Given that set-up, I want to create a custom Facade class called Helpers so that I can use it for various operations in my app, for e.g. Helpers::getFreeStock();
To achieve this, I have created the following folders & files:
with the following codes in the respective files:
app/Latheesan/System/Helpers/Facades/Helpers.php
<?php namespace Latheesan\System\Helpers;
use Illuminate\Support\Facades\Facade;
class Helpers extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'helpers'; }
}
app/Latheesan/System/Helpers/Helpers.php
<?php namespace Latheesan\System\Helpers;
class Helpers {
/**
* Get free stock levels by organisation id.
* If param $skus is empty, free stock will be calculated for every inventory organisation owns.
*
* @param $organisation_id
* @param array $skus
* @return array
*/
public function getFreeStock($organisation_id, $skus = [])
{
return 'hello world - todo actual implementation';
}
}
app/Latheesan/System/Helpers/HelpersServiceProvider.php
<?php namespace Latheesan\System\Helpers;
use Illuminate\Support\ServiceProvider;
class HelpersServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['helpers'] = $this->app->share(function($app)
{
return new Helpers;
});
}
}
Now I have updated the app/config/app.php like so:
// Add service provider
'Latheesan\System\Helpers\HelpersServiceProvider'
...
// Create alias
'Helpers' => 'Latheesan\System\Helpers\Facades\Helpers'
Finally, to test this, I created the following route:
Route::get('/test', function() {
dd(Helpers::getFreeStock(1, ['vcf001']));
});
When I visit this /test
to test the helpers Facade class, I get the following error:
Class 'Latheesan\System\Helpers\Facades\Helpers' not found
What have I done wrong here? Any idea why this isn't working?