I need to override Auth::user() for all drivers.
I created a class 'MyGuard' that override Illuminate\Auth\Guard, but I don't know how to say to the entire application to use MyGuard instead of Illuminate\Auth\Guard.
Probably i can't do that because Guard is directly instantiated in Illuminate\Auth\AuthManager using
new Guard($provider, ...).
Auth::extends() seems to serve only to create new drivers.
I don't want to write directly inside original Guard class.
How can I solve this problem?
Related
I created a model in Laravel. I always noticed the default would be use HasFactory. May I know what exactly does it work. In my understanding from reading documentation, it is for linking to database (I guess?) But I still don't understand how it works exactly.
HasFactory is not to link to the database.
It is a trait that links a Eloquent model to a model factory.
Factories are normally used in testing when wanted test-data for a specific model.
You can read more about factories in Laravel here: https://laravel.com/docs/9.x/database-testing#model-factories and here:
https://laravel.com/docs/9.x/eloquent-factories
The trait ensures that you can instantiate a factory like this:
User::factory()->create();
In older versions of Laravel the trait was not used, and instead a factory had to be instantiated by the global factory helper like this factory(User::class)->create(); but that caused a lot of problems with intellisense in IDE's.
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 am new to zend framework 3 and I am trying to create a new route type that can search in the database to match the route path. I am using doctrine orm and unfortunately I don't know how to inject the entity manager inside the route class.
I tried defining a factory class for the route to have access to the service manager but that didn't work because the route classes must implement Zend\Router\Http\RouteInterface which states that the route class must contain it's own factory defined as "function factory($options)".
Can anyone please help?
Thank you very much.
In the way you ask the question you have to write your custom Router strategy relying on zend-router's interfaces and abstractions.
For eg. ZF support different router strategies to match the URL (as in any modern framework) but you need to write the custom router strategy to access the DB and return what controller/action should be executed.
To be honest if you are new to ZF3 maybe try to solve the problem on little bit less advanced way.
Other solution, maybe you can create dynamic router and pass URL_SLUG.
Than you will have one action where you will check what content you should load from the DB by URL_SLUG (or any other ID you choose).
I'm trying to implement the new Laravel 5.3 auth:api middleware using api tokens. Since I already have a legacy users table containing an 'ApiToken' field I would like to change the TokenGuard storageKey property (and possibly also the inputKey) to "ApiToken" instead of 'api_token'. Similar to the getRememberTokenName method, I couldn't find a 'getApiTokenName' method however. Is there a way to accomplish this without altering the Laravel TokenGuard source?
You cannot override the storageKey in TokenGuard class.
But you have 2 options to solve your problem:
Create your custom guard which should be extended from TokenGuard and inside it change the 'storageKey' property. And then register your custom guard inside AuthServiceProvider as your guard and use them.
Change 'ApiToken' field in your table to snake_case.
I am just about three days old in laravel, yesterday I tried creating an authentications system using eloquent, so without looking I deleted the default User model, and then I tried creating my own from what I had read from the documentation. After setting up every thing as I had studied and understood, I tried running my app, but whenever I enter the correct username and password I get this error
Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials()
must be an instance of Illuminate\Auth\UserInterface, instance of User given, called in dir
I did not know what the EloquentUserProvider was or where it was even coming from. My original model looked like this
class User extends Eloquent {}
I battled with this error for the whole day (no exaggeration), But today I found out from this SO Can't authenticate user in laravel that it was because I had not implemented some interface, so they linked me to https://github.com/laravel/laravel/blob/master/app/models/User.php where I copied the default model I initially deleted.
I used the documentation almost through out my learning process, and no where in the documention for authentication and for eloquent did they mention that we are suppose to implement these interface for us to be able to use Auth::attempt() method, my question now is how then do we (newbies) know what to implement and what not to implement, or what any of these interfaces even do.
This is simple. It's a laravel's requirement. The User model is generated by default for you. If you do not need to implement the interface's methods, just add them empty in your User class.
And of course, in your case, what to extend or implement will be shown as errors on startup. Reading them carefully can give you all the asnwers.
Also, if you want to use different User Authentication features, or extend the existing ones, you can look some info here in the docs