Last active
December 26, 2015 01:19
-
-
Save Landish/7070707 to your computer and use it in GitHub Desktop.
Laravel 4 Custom Facades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/config/app.php | |
'providers' => array( | |
'...', | |
'MyNameSpace\MyClassServiceProvider' | |
), | |
'aliases' => array( | |
'...', | |
'MyClass' => 'MyNameSpace\Facades\MyClass' | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// MyNameSpace/MyClass/MyClass.php | |
namespace MyNameSpace; | |
class MyClass { | |
public function myMethod() | |
{ | |
echo 'Welcome to MyClass/myMethod'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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