I have an laravel 5.8 app, with the default auth installed. I want to pass a variable to the password reset email template, so the greeting would look like "Hello, [username]".
The current email template looks like this:
I didn't find where in the core files I can change this "Hello!" heading of the message
EDIT:
In my controller, I send the password reset like this(maybe the variable should be put somewhere on this line)
$response = Password::sendResetLink($credentials, function (Message $message) {
$message->subject($this->getEmailSubject());
});
In a default Laravel app, the User model has a CanResetPassword interface with a sendPasswordResetNotification method. This method is created in a CanResetPassword trait that the user also uses.
You could override this with your own method and create your own ResetPasswordNotification class. Try following it from the trait to that class.
It does not look like it uses a view for the markup, so it shouldn't be too hard to do. The ResetPassword class takes accepts a Notifiable, so you should have access to the name in there.
Related
I am building a Laravel 7.x. application that will combine both standard authentication (user logs on via form) and API authentication through Sanctum (token based). I want to generate a sanctum API token during successful user authentication. To do that, I need to hook into the login flow.
Standard authentication was scaffolded to my application by running php artisan ui vue --auth.
When I inspect routes/web.php, I can see only Auth::routes(); which under the hood allegedly generates the classic routes I was used to in previous Laravel versions. Taken from the answer I linked, /login route definition that is generated looks like this:
$this->post('login', 'Auth\LoginController#login');
However, when I inspect my LoginController that was scaffolded, I can not see any of the methods that should be generated by Auth::routes(). There is nothing in there and it looks like everything is handled transparently to me as a developer:
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
How do I hook into the login flow and add my own actions to it?
I can see, two questions here and I will try to answer them as good as I can.
1. Execute action on successful user authentication
I think the cleanest way to achieve this, it to utilize the Laravel event / listeners architecture.
In app/Providers/EventServiceProvider.php extend the $listen array
// include at the top
use Illuminate\Auth\Events\Login;
protected $listen = [
// other handlers [],
Login::class => [
CreateUserApiToken::class,
],
];
Then execute this artisan command, which will magically create your listener file
php artisan event:generate
Now you can open app/Providers/CreateUserApiToken.php and put whatever you like in the handle function.
public function handle(Login $event)
{
dd($event->user);
}
2. Where's the actual laravel code?
For lots of Laravel classes, you will only find a minimal amount of code projected directly to your app directory. Most of it is hidden in traits or by extending parent classes. Let's take the LoginController for example
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use AuthenticatesUsers;
This is the trait that controller is using. And especially the top line gives you a pretty good clue where that file is located. You could also use your code editors search function, to search in all files for trait AuthenticatesUsers.
In this case the corresponding file would be vendor/laravel/ui/auth-backend/AuthenticatesUsers.php. Here you will find most of the functions that you are looking for. Of course, it's not a good idea to overwrite that file directly, because all the changes would be lost, if the laravel framework get's updated.
But if you find a function in there, that you want to change, let's say showLoginForm() for example, you can simply include that function in your LoginController and change the code.
public function showLoginForm()
{
return view('example.login');
}
i am working on a laravel project .almost i see in each controller these line format :
!auth()->user()->can('something (differ from each controller to other one)')
but in my php editor it says method {can} not found for this object. so i try to found method can and not found it .even i edit __Call magic method to see if when can method is calling does magic function run but i know it never run for can function.so how it is possible to link function to object when it is not defined in class and its all mother class.and where does can function locate?i search and i see laravel has some policy for authorize users but yet i don not know how can function link to user object without
define in classes and even magic method does not run .and how can i develope these type of function (for authorizing in laravel and change policy)
can method is a part of Authorizable trait in Illuminate\Foundation\Auth\Access
Look at your User model, you will found that model is extended from Illuminate\Foundation\Auth\User as Authenticatable. and Authenticatable uses the above trait.
it's part of the core of laravel, you can read the documentation from official docs
I had anonymous component resources\views\components\homepage\feedback.blade.php to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
class Feedback extends Component
{
public $feedbacks;
public function __construct()
{
$this->feedbacks = Feedback::wherePublished(true)->take(5);
}
public function render()
{
return view('components.homepage.feedback');
}
}
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:\laragon\www\lara7\resources\views\components\homepage\feedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
I found problem.
I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\views\components\homepage\feedback.blade.php and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.
So laravel tried to render resources\views\components\homepage\feedback.blade.php with $feedback variable inside and it cannot find where $feedback is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/> to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working.
But it doesn't say that inside components subfolder automatic discovery is not working.
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue
Continuing with my Laravel 5.5 project, I'm confused as to where you define the slack webhook URL in the Laravel app.
https://laravel.com/docs/5.5/notifications#routing-slack-notifications shows defining a routeNotificationForSlack method on the User (or whatever notifiable model) that returns $this->slack_webhook_url-- but that var is never populated:
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForSlack()
{
return $this->slack_webhook_url;
}
Should the slack_webhook_url be hardcoded in this model, or is it passed in when you instantiate the notification?
it's up to you how to implement routeNotificationForSlack(), Laravel will only call it and use the return value to determine what url to ping.
If all your users share same webhook endpoint, you can store it in .env, if they are different for each user, return a database column like the example. If it depends on group/privilege, you should implement it accordingly
I need to be able to do a post method to the default "/register" from a form even when im already authenticated.
Currently its not possible, I get no errors but I don't get a record in my database.
My form and code works when im not logged in.
Where do i need to make changes so it will work?
I think its near the registersusers.php but that code is completely new to me.
By default you cannot do this because the guest middleware stops logged in users from accessing the registration page.
In your AuthController (provided by Laravel in app/Http/Controllers/Auth) you can specify which methods should remain accessible to non-guests (or authenticated users).
By default only the logout method is available to logged in users, however you can add anymore you wish.
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => ['logout', 'showRegistrationForm', 'register']]);
}
showRegistrationForm is responsible for showing the login page and form, register is responsible for processing the registration and persisting it.
Technically, it's working as intended. A logged in user shouldn't be able to register a user, that doesn't make sense.
However, you can simply use the RegistersUsers trait within your own Controller in order to attempt to register a user. It's as simple as this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RegistrationController extends Controller {
uses RegistersUsers;
public function create(Request $request){
return $this->postRegister($request);
}
}
The ->postRegister() method is provided by the RegistersUsers trait. It accepts an \Illuminate\Http\Request object and is injected as a dependency, so you must only pass an instance of \Illuminate\Http\Request. You can easily display the registration form as well by simply just calling $this->showRegistrationForm();, such as this:
public function show(){
return $this->getRegister();
}
Now just attach your routes accordingly.