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.
Related
I am trying to use Laravel 5.2's RESTful resource controllers. However, when moving from my index to create, I would like to pass a parameter as the create page should be partially filled in.
edit
The form that will be 'created' will have populated fields from the database already. So the create should take the id from the user that is clicked in the index.
My temporary solution:
Route::get('consultation/{id}', 'ConsultationController#create');
Route::resource('consultation', 'ConsultationController', ['except' => ['create']]);
Is there a way to add this to an options array in the same line as resource?
Thanks
Edit: I suppose in this case, my store would also need the same {id} parameter.
I was also curious about that but I think that the Laravel Documentation is pretty clear about this:
If it becomes necessary to add additional routes to a resource
controller beyond the default resource routes, you should define those
routes before your call to Route::resource
And they add the following, meaning for me that if you want to override a defined route, you just need to put the definition on top of the Route::resource() definition.
otherwise, the routes defined by the resource method may
unintentionally take precedence over your supplemental routes
EDIT
After better understanding the question, I would let the restful controller as is, and create a new route like /user/{user-id}/consultations/create that is much more "restful" like.
I have recently started using laravel 5.2. I don't understand what's the use of guards. As I see everything that can be done with guard can easily be replaced with lot more readable middleware. Am I missing anything ?
Guard will let you to have multi authentication in your app and you we have diffrent provider even drivers(such as doctrine or eloquent). for example, you can have login with user model and an admin model in one application at the same time.
You may specify which guard instance you would like to utilize using the guard method on the Auth facade. This allows you to manage authentication for separate parts of your application using entirely separate authenticatable models or user tables.
The guard name passed to the guard method should correspond to one of the guards configured in your auth.php configuration file:
if (Auth::guard('admin')->attempt($credentials)) {
//
}
Know more about Illuminate/Contracts/Auth/Guard
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.
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
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?