http://laravel.com/docs/5.1/facades
Facades are listed on the linked page at the bottom. My question is... How do I override these service container bindings?
Example, the Request facade binds Illuminate\Http\Request with the key request. I want to create my own class which inherits from Illuminate\Http\Request and bind it with the request key, instead of the current class. And I can't find which service provider binds this.
So, I kind of did it. First I noticed these bindings were hardcoded in Illuminate\Foundation\Application::registerCoreContainerAliases() so I extended this class and overrode this method to change it. I also had to call this class now in bootstrap\app.php, but doing all this didn't help, I was still getting an instance of Illuminate\Http\Request.
So then I discovered that Illuminate\Http\Request was directly referenced in public\index.php so I tried changing it there to My\Very\Own\Http\Request and this worked, finally my implementation was being used.
Finally, I deleted my version of Application::registedCoreContainerAliases() and reverted bootstrap\app.php because everything is also working without this change.
I think the following answer would be very useful for you
https://stackoverflow.com/a/39648307/3912276
I quoted the most important part of the answer. It explains how you can replace/extend the Mailer facade
Write your own implementation of Mailer, extending Illuminate\Mail\Mailer, in which you can override the send method, implement your checks and call parent::send().
Write your own service provider (Extending Illuminate\Mail\MailServiceProvider), in particular re-implement the register method. It should create an instance of your own Mailer in place of Laravel's own. (You can copy most of the code from Laravel's register method).
Now, in your config/app.php file, in the providers array, replace Illuminate\Mail\MailServiceProvider::class, with your own provider.
Related
Hi I have problem with Attribute class.
I created attribute class that takes RequestStack object as an argument. I need to check in this class if user was binded to request attributes, but there is no way to autowire it or pass an instance. If I pass it like, new RequestStack(), then I don't have access to any request, but there is no exception.
How can I fix it. I wanna use it like the symfony security bundle do it, Fe.
#[IsGranted('ROLE_USER')]
I tried:
configuring services.yaml
configuring in services.yaml arguments and binding service to it
rewriting it to annotaion, but result is the same
instancing RequestStack inside attribute class
I'm currently thinking to use reflection class, but I'm not sure if the RequestStack is going to contain all the requests (also the original/main one). It's important to me to keep track the request instance.
There are many questions regarding loading custom helper classes in Laravel. However, none of them focus on the loading them with proper initialization.
As of Laravel version 5.3 we can use psr-4 autoloading which is autoloading the entire app/ directory. However, classes are loading but never initialized.
I have my helper class inside the app/helpers/support.php. This class has a constructor, where I want to load some important configuration in order to make the helper usable.
So how can I load my helper but ALSO initialize it properly in Laravel? Right now I am simply working-around the problem by using new \App\Helper\Support(); inside AppServiceProvider.php.
Edit: I'm using the following approach to maintain my helper class:
Best practices for custom helpers on Laravel 5
It seems like what you have is a service. Rather than creating an instance, you can declare it in your app service provider and inject it as a dependency when you need it.
In your register method:
$this->app->bind(\App\Helper\Support::class);
You can now use dependency injection to get an instance of your class. You can also make an instance like this:
app()->make(\App\Helper\Support::class);
If you only want one instance to exist at any given time, use singleton rather than bind.
I recommend reading the service container documentation:
https://laravel.com/docs/5.5/container
I implemented a role/permission system. Now I want to add a method to Laravel router so I can make something like this:
Route::get('sales', 'SaleController#index')->allow('Salesman');
I know that I can use #can('Salesman') (View) and $user->can('Salesman') (Controller) but I found so much readable the way I'm trying to do it since I'll be able to see all role permission access in the routes file.
You can override the router class, then register it into service container to be used by Route facade.
To be more clear:
Write a class that extends Laravel's router (I think Router class). To find this, open the Route facade, then find its service provider. From there, it should be easy to find the router class.
Write a class that overwrites that router. Make sure to extend the class you found before.
Write a service provider that overwrites the router services. The practically means to register your service under the same key name you find in Route facade.
And that should be it. Your service is now picked by Route facade automatically.
As you're using the facade to generate the routes. This should be quite easy. The facade can be overruled in the config/app.php facades array.
You can generate your own Facade class and replace the native one with yours. Which in fact is a Router class. In order to implement the functionality you need to extend and override the following in sequence:
Facade
Router::newRoute
Route
By extending the last one and returning those in the newRoute method of the Router, you'll be able to overrule the logic of Laravel.
I'm building an app with Laravel 5.1 using my own service providers to separate some business logic, but I can't seem to find a way to acces other methods of the service provider from within itself.
Using the function name tries to look for something in the namespace App\Providers and there's no $this because it's singleton pattern, so how can I do it? Does it have to "use" itself?
I also don't want an external helper as this function would be specific to this service.
Simply enough it can be done using self::method() or static::method() but your method has to be declared as static.
I've been using Laravel for a long time, and I'm now writing a micro-project using Lumen.
I need to pass some variables to all views. In Laravel I can use the View::share() function in a middleware or in the controller's constructor, but in Lumen there is no View class, and it looks like all view functionality is simply View::make() alias.
Is there a way to share variables to all views?
For performance reasons, Lumen doesn't register facades and service providers the way Laravel does. While the Laravel facades are included with Lumen, only some are aliased (View not being one of them), and only if you uncomment the $app->withFacedes(); line in bootstrap/app.php (you can check the Laravel\Lumen\Application::withFacades method to see which ones). So in order to use other facades such as View, you either need to alias the facade class yourself:
// "bootstrap/app.php" is a good place to add this
class_alias('Illuminate\Support\Facades\View', 'View');
Or you can include it with use wherever needed:
use Illuminate\Support\Facades\View;
The correct way to share data with views in Lumen is:
app('view')->share(...);
Some of Laravel's functionality that is not explicitly described in Lumen documentation can be accessed with Lumen's app() helper function.