Created
March 10, 2016 11:39
-
-
Save gradosevic/ccb9f5f96dd3f98b5248 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/////////////////////////////////////////////////// | |
// STEP 1 - CREATE CLASS THAT WILL BE USED GLOBALY | |
/////////////////////////////////////////////////// | |
namespace App\MyApp; | |
class MyApp { | |
public function sayHello($data = []) | |
{ | |
echo "Hello World from Facade!"; | |
} | |
} | |
///////////////////////////////////////////////////////// | |
// STEP 2 - CREATE SERVICE PROVIDER CLASS | |
///////////////////////////////////////////////////////// | |
php artisan make:provider 'MyAppServiceProvider' | |
/////////////////////////////////////////////////////////// | |
// STEP 3 - ADD REGISTER METHOD TO SERVICE PROVIDER CLASS | |
/////////////////////////////////////////////////////////// | |
namespace App\Providers; | |
use Illuminate\Support\Facades\App; | |
use Illuminate\Support\ServiceProvider; | |
class MyAppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register the application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
App::bind('myapp', function() | |
{ | |
return new \App\MyApp\MyApp; | |
}); | |
} | |
} | |
/////////////////////////////////////////////////// | |
// STEP 4 - CREATE FACADE CLASS | |
// Create: App\Facades\MyApp.php | |
/////////////////////////////////////////////////// | |
namespace App\Facades; | |
use Illuminate\Support\Facades\Facade; | |
class MyApp extends Facade{ | |
protected static function getFacadeAccessor() { return 'myapp'; } | |
} | |
///////////////////////////////////////////////////////////// | |
// STEP 5 - ADD ALIAS AND SERVICE PROVIDER TO config/app.php | |
//////////////////////////////////////////////////////////// | |
//Add service provider | |
App\Providers\MyAppServiceProvider::class, | |
//Add alias | |
'MyApp' => App\Facades\MyApp::class | |
/////////////////////////////////////////////////// | |
// STEP 6 - TESTING | |
/////////////////////////////////////////////////// | |
MyApp::sayHello(); | |
//Output: Hello World from Facade! | |
Nice, thank you.
GREAT!! Tanks, it works fine :)
It's not working.
Also when using App::bind
App::bind('myapp', function() { return new \App\MyApp\MyApp; });
It gives an error
Method 'bind' not found in \Illuminate\Support\Facades\App
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice and please add another step to dump composer autoload by typing:
composer dump-auto