Skip to content

Instantly share code, notes, and snippets.

@Landish
Last active December 26, 2015 01:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Landish/7070707 to your computer and use it in GitHub Desktop.
Save Landish/7070707 to your computer and use it in GitHub Desktop.
Laravel 4 Custom Facades
<?php
// app/config/app.php
'providers' => array(
'...',
'MyNameSpace\MyClassServiceProvider'
),
'aliases' => array(
'...',
'MyClass' => 'MyNameSpace\Facades\MyClass'
)
1) Copy the /MyNameSpace/ folder in your /app/libraries/ directory
2) Create below listed files in /app/libraries/MyNameSpace/ directory and write your code.
3) Add "app/libraries" to autoload classmap object in composer.json file
4) Run "composer dump-autoload" command with your CLI.
5) Add Route::get('test', function() { return Myclass::myMethod(); }); closure function to your app/routes.php file.
6) Visit to page http://laravel/index.php/test and see the code in action.
<?php
// MyNameSpace/MyClass/MyClass.php
namespace MyNameSpace;
class MyClass {
public function myMethod()
{
echo 'Welcome to MyClass/myMethod';
}
}
<?php
// MyNameSpace/MyClass/Facades/MyClass.php
namespace MyNameSpace\Facades;
use Illuminate\Support\Facades\Facade;
class MyClass extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'myclass'; }
}
<?php
// MyNameSpace/MyClass/MyClassServiceProvider.php
namespace MyNameSpace;
use Illuminate\Support\ServiceProvider;
class MyClassServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('myclass', function()
{
return new MyClass;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment