John Davidson

php - Overriding Laravel's method

0 comments
Message:


Given the route:


 Route::get('verify/{id}/{hash}', 'Auth\VerificationController@verify');

It uses the Laravel's default verify method from


auth-backend/VerifiesEmails.php

The default verify method looks like bellow:


public function verify(Request $request)
{

if (! hash_equals((string) $request->route('id'), (string) $request->user()->getKey())) {
throw new AuthorizationException;
}

if (! hash_equals((string) $request->route('hash'), sha1($request->user()->getEmailForVerification()))) {
throw new AuthorizationException;
}

if ($request->user()->hasVerifiedEmail()) {
return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath());
}

if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}

if ($response = $this->verified($request)) {
return $response;
}

return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath())->with('verified', true);
}

I would like to change only the last block of the code in the verify method from


        return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath())->with('verified', true);

to


    return $request->wantsJson()
? new Response('', 204)
: redirect($this->redirectPath())->with([
'verified' => true,
'userNotification' => [
'message' => 'Wellcome to my website',
'title' => 'Hello World',
],
]);

I know I can override the whole verify method in the VerificationController, which is not ideal to copy and paste the whole block of code for a small change.
My question is How can override only the last block of code as mentioned above?



My Answer:
In Laravel, you can override default functionality by extending the core classes and modifying their behavior. Here's how you can override Laravel functionality:

1. Create a new class that extends the core class you want to override. For example, if you want to override the default User model, you can create a new class called CustomUser that extends the User model.

php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CustomUser extends User
{
// Add your custom functionality here
}


2. Update the configuration to use your custom class instead of the default one. For example, if you want to use your CustomUser class instead of the default User model, you can update the configuration in your application:

php
// config/auth.php

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\CustomUser::class,
],
],


3. Make sure to update any references to the core class in your application code to use your custom class instead. This includes any controllers, services, or other classes that interact with the overridden functionality.

By following these steps, you can effectively override Laravel functionality and customize the behavior of your application to suit your needs.

Rate this post

3 of 5 based on 4811 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us