How can I override the forgot password email from address field?
I'm using the SendsPasswordResetEmails trait.
It seems to be using the .env mail-from configuration
here is the trait vendor code SendsPasswordResetEmail Trait
sendResetLinkEmail method seems to be where the magic happens but i cannot determine how to override the mail send from the broker where is this function? sendResetLink
You can just copy the trait its code and past it in the PasswordBroker class to overwrite it if that's what you're asking.
I think you don't have to edit broker() just override sendResetLinkEmail() in your ForgetPasswordController. Then override $request->mail entry.
Anyway, the function you are looking for is at "\vendor\laravel\framework\src\Illuminate\Auth\Passwords\PasswordBroker.php" => sendResetLink()
I think what you are trying to do is set
MAIL_FROM_ADDRESS=sender#example.com
MAIL_FROM_NAME=Sender
in .env file and it should work fine, you do not need to override sendResetLinkEmail method.
There are a two methods I can think of that will achieve what you want.
1) In AppServiceProvider:
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
config()->set('mail.from.address', 'YOUR FROM ADDRESS HERE');
}
2) In Controller.php:
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
config()->set('mail.from.address', 'YOUR FROM ADDRESS HERE');
}
}
Hope that helps!
Related
I'm new to this service container stuff in general.
Just looking for an easy way to override the getView() method of the Captcha class.
My idea was to create a new class extending the captcha class:
<?php
namespace App\Http\Helpers\Captcha;
use Igoshev\Captcha\Captcha\Storage\StorageInterface;
use Igoshev\Captcha\Captcha\Generator\GeneratorInterface;
use Igoshev\Captcha\Captcha\Code\CodeInterface;
class CaptchaNew extends Igoshev\Captcha\Captcha
{
/**
* Get html image tag.
*
* #return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function getView()
{
//new code...
}
}
Inside the AppServiceProvider under register method using:
$loader = AliasLoader::getInstance();
$loader->alias('App\Http\Helpers\Captcha\CaptchaNew', 'Igoshev\Captcha\Captcha');
I already tried the boot method too, doesn't work too. What's the best way to override the class? A `serviceProvider' is provided too, but I want to keep things simple and I have no idea about serviceProviders in general.
You could try this way below to register your service, use bind function instead.
Under your register function in AppServiceProvider, replace yours with the sample code below. But please be noticed that first parameter of bind function must be same as facade accessor of vendor library (in this case is \Igoshev\Captcha\Captcha\Captcha::class, you can take a look here). Register this facade in config/app.php after the service provider of package:
$this->app->bind(\Igoshev\Captcha\Captcha\Captcha::class, function(){
return new CaptchaNew($this->app['config']);
})
I am trying to pass data from login controller to login.blade.php in laravel 6 like I using common header throughout the application so I have to create dynamic title and description. default login controller is something like this
class LoginController extends Controller
{
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
Don't know how to pass. please help
Just customize your showLoginForm function provided by laravel as default in your LoginController. see code below
class LoginController extends Controller {
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function showLoginForm()
{
return view('login', ["data"=> 'this is test data']);
}
}
If you are looking to learn Laravel, I would recommend checking out the guides in their documentation here: https://laravel.com/docs/6.x
Relating to authentication, there is a short guide here that will get you up and running quickly: https://laravel.com/docs/6.x/authentication
You can use the two commands below to setup all of the framework and routing that you need to handle authentication automatically, it will also give you an idea of what to look at if you choose to build your own authentication in future.
composer require laravel/ui --dev
php artisan ui vue --auth
Well, there should have a Trait named AuthenticatesUsers onto the LoginController.php in Laravel 6.x. But somehow we don't see it in the login controller you provided above.
The Trait is located here
vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
However there is a method called showLoginForm(). You can override this method and pass whatever you need to pass in as the following.
/**
* Show the application's login form.
*
* #return \Illuminate\Http\Response
*/
public function showLoginForm()
{
// Notice the second argument
return view('auth.login', ['key' => 'value']);
}
Hope this would make sense.
Following the guide to implement the native mail verification of laravel.
Brings me an error.
Note please that i use MongoDB, therefore i'm using Jensseger/laravel-mongodb package
This is the error:
Class App\User contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\MustVerifyEmail::hasVerifiedEmail, Illuminate\Contracts\Auth\MustVerifyEmail::markEmailAsVerified, Illuminate\Contracts\Auth\MustVerifyEmail::sendEmailVerificationNotification
I've already try to implement the methods inside my model and they seem to solve the problem. But it won't send any emails.
Here's what i've implemented im my User.php model
* Determine if the user has verified their email address.
*
* #return bool
*/
public function hasVerifiedEmail()
{}
/**
* Mark the given user's email as verified.
*
* #return bool
*/
public function markEmailAsVerified()
{}
/**
* Send the email verification notification.
*
* #return void
*/
public function sendEmailVerificationNotification()
{}
Here's my User.php model
namespace App;
use App\Company;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
protected $connection = 'mongodb';
Here's my web.php route file.
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['verify' => true]);
Route::get('/home', 'HomeController#index')->name('home');
And here's my HomeController.php
public function __construct()
{
$this->middleware(['auth','verified']);
}
Here's my env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=25
MAIL_USERNAME=xxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxx
MAIL_ENCRYPTION=tls
Like this the project work but it wont send emails. Do i need to put the logic inside the three method inside the User.php? If yes what should i put in it? I've no idea because if it's native and work like charm with SQL i don't really know how to get it work on my project
Hope someone has a solution for this.
Thanks
Easiest solution is to implement trait Illuminate\Auth\MustVerifyEmail which should be there, however it is not mentioned in the Laravel documentation. You can also override these methods by defining them in the model as you did. But hasVerifiedEmail and markEmailAsVerified methods should have some verification logic and return bool based on the API.
Edit:
I also forgot to mention that method sendEmailVerificationNotification should contain $this->notify(new Notifications\VerifyEmail); otherwise it won't use the Notifiable trait and thus not send any email. For more details take a look at the method in Laravel framework repository,
I am trying to make my own custom Facade and register is with a custom service container and finally creating a custom alias for this facade.
I am not sure what part is not working, maybe there is a problem with the service container registering or maybe with the alias?
Let's start with my facade:
/**
*
* #see \App\Library\Facades\ViewWrapper\CustomView
*/
class CustomViewFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'customview';
}
}
My CustomView class with the logic and the show function
namespace App\Library\Facades\ViewWrapper;
...
class CustomView
{
public function show(...) { ... }
...
}
My CustomViewServiceProvider
namespace App\Providers;
...
class CustomViewServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(CustomViewFacade::class);
$this->app->alias(CustomViewFacade::class, 'customview');
}
}
How I register the provider in the config\app.php
App\Providers\CustomViewServiceProvider::class,
How I create the alias in the config\app.php
'CustomView' => App\Library\Facades\ViewWrapper\CustomViewFacade::class
In my controller I use the facade like this:
use CustomView;
...
public function show(ImageRequest $imagerequest)
{
return CustomView::show(...);
}
I get the following error in the controller:
Class 'CustomView' not found
What am I doing wrong here?
EDIT
After clearing config and composer autoload dump I get the following error:
Call to undefined method App\Library\Facades\ViewWrapper\CustomViewFacade::show()
I think you haven't quite clearly understood how Facades work. They are just an easy way to access your services without having to deal with dependency injection. I'm not a fan of this methodology, but here's how you do it properly.
You need to bind your actual service to the container, not the facade. The facade is almost just a symbolic link to your service within the container.
You need to import the actual service, not the facade. Laravel will automatically bind your dependency in the type-hinted variable, thanks to its behind the scenes magic.
Use:
use App\Library\Facades\ViewWrapper\CustomView;
(small note: your namespace here should be your service's namespace, be aware to not mix up the semantic between facade and service. The service contains the logic, the facade is just an accessor to a service that is already injected. This is important!!)
Instead of:
use CustomView;
It should solve the issue.
Also, I'd suggest you do define how the class should be constructed and injected in the Service Container by using a Closure in the bootstrap function.
class CustomViewServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->singleton(CustomView::class, function () {
return new CustomView(...);
);
}
}
Also, the alias function is not necessary in your case. It'd simply allow you to access the service by using the customview key in the Service Container.
Just define the Facade in your config/app.php file.
Another small suggestion: use PHP 7 class selectors instead of strings in your facade accessor definition. For example: CustomView::class intead of customview. It makes your code neater and easier to read.
Please run below command and check:
php artisan config:cache
php artisan cache:clear
Here I began with Silex and I do not understand how to override a class.
I use a plugin to manage authentication.
Everything works, however I would like to change the checkPreAuth () method to verify the account from the user before authentication without change yhe bundle code.
To do this I made the steps below:
I created a class UserDAO to call the plugin which extends the native class.
But it doesn't work because I make a simple die('foo !') in the new method in UserDAO but apparently my method is not taken into account because the die('foo !') doesn't appear :-( someone there to help me :-)
Class UserDAO:
use MicroCMS\Domain\User;
use MicroCMS\PDO\PDOController as PDOController;
use PDO;
class UserDAO extends BaseUserChecker implements UserProviderInterface
{
public function __construct() {
}
/**
* {#inheritDoc}
*/
public function checkPreAuth(UserInterface $user){
die('je rentre dans cette fonction !');
}
public function checkPostAuth(UserInterface $user){
}
}
thanks in advance