How to override a Laravel package function - laravel

I'm using https://github.com/artesaos/seotools package for seo.
I am trying to override getCanonical function located at https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php and make it's output as lowercase. Could you please guide me how can I do this?

You can try following :
Step 1:
Create a child class extending SEOMeta class and override the getCanonical function.
Class XyzSEOMeta extends SEOMeta {
public function getCanonical () {
// Write your logic here
}
}
Step 2:
Create the Service Provider for overridden class. First parameter of bind function must be same as facade accessor of SEOMeta Facade (check here). Register this facade in config/app.php after the service provider of seotools package. :
Class XyzSEOMetaServiceProvider extends ServiceProvider {
public function register(){
$this->app->bind('seotools.metatags', function(){
return new XyzSEOMeta($this->app['config']);
})
}
}
You are all set. Hope this will help.
EDIT:
Above mention method will just override the single class. If you want to change the logic of more than one class. Best way is to fork the project. Change the code and push it to your fork. Use forked project as your composer dependency. Follow the link to know how to use private repository as composer dependency : https://getcomposer.org/doc/05-repositories.md#using-private-repositories

It's very simple just like we overriding any parent class function in derived class.
Create your own class and extend your package class SEOMeta and re-declare function that you want to override and put your logic inside. Don't forget to use namespace of your package class SEOMeta at upper your custom class.
Now use your custom class instead of package class inside your controller.
e.g
use Path\to\SEOMeta;
class urclassname extends SEOMeta{
public function overridemethodname(){
// put ur logic here.
}
}

Related

Override vendor model in laravel 6.X

I want to override the model inside of vendor. I tried bellow code. But not working.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
$this->app->bind('VendorName\Models\User', 'App\Models\User');
}
}
Extending model is not an option, as i have to override all controller change model path and write all methods again, its not worth it.
Binding a class in the container like this isn't going to override direct references to the class you're trying to override if the object isn't being resolved using the service container.
So for example, something like $user = new \VendorName\Models\User; isn't going to be affected because it's simply not using the container.
I think the only sensible solution is to refactor your code so you're using a class that extends the base User class.

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

What does the make() method do in Laravel?

In the Laravel documentation, I found the following - https://laravel.com/docs/5.4/container#the-make-method
but I am still confused as to what exactly the make() method does. I know the create() method uses the make() method and then persists them into the database, so does make() methods just temporarily save it in php tinker or something? Sorry, I'm Laravel noob. I'm trying to figure out these functions. Thank you! :)
The make method will return an instance of the class or interface you request.
Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class.
E.g.
$app->make('App\Services\MyService'); // new \App\Services\MyService.
One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.
E.g. an instance of the Mailer class would be automatically injected here.
namespace App\Services;
use \Illuminate\Mail\Mailer;
class MyService
{
public function __construct(Mailer $mailer) {
$this->mailer = new Mailer;
}
}
I discovered recently that when you use make (), you are installing the class and you can access the methods of that class or model, this is a useful for the Test and validate that you are getting what you want Example:
User model
class User extends Authenticatable
{
public function getRouteKeyName ()
     {
         return 'name';
     }
}
Test user
class UserTest extends TestCase
{
public function route_key_name_is_set_to_name ()
     {
$ user = factory (User :: class) -> make ();
$ this-> assertEquals ('name', $ user-> getRouteKeyName ());
// When you access the getRouteKeyName method you get the return, that is 'name'
}
}
On the other hand if you use "create" that would give an error because you are creating a user

Method [validate] does not exist

Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[
You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.

Dependency Injection on Laravel 4

When we are in the need to access another class within a repository , the below technique works as expected.
ie.
namespace App;
class AbcRepo implements AbcInterface {
}
namespace App;
class DefRepo implements DefInterface {
protected $abc;
public function __construct(AbcInterface $abc) {
$this->abc = $abc;
}
}
So when I register this in the service provider :
$app->bind('App\DefInterface', function($app) {
return new App\DefRepo(App::make('App\AbcInterface'));
});
The question that's bothering me is doing this :
new App\DefRepo(App::make('App\AbcInterface'));
Is it the correct approach. We (i mean I) never unit test a service provide , so I could simply ignore as this works. But any input would be appreciated.
It is correct, however it is better to let typehinted dependency injection initialize RefRepo.
$app->bind('App\DefInterface', 'App\DefRepo');
$app->bind('App\AbcInterface', 'App\AbcRepo');
That means Laravel will try to initialize DefRepo when a class that implements DefInterface is needed. Because DefRepo needs AbcInterface the class AbcRepo will be injected into the instance.

Resources