I want to override
vendor\laravel\framework\src\Illuminate\Auth\Password\DatabaseTokenRepository.php
I tried this user model in app folder.. but that is not working.. Can you tell me where to put it?
So see this is a vendor class. If you want to override any functionality of that class you can do so by applying method overriding. Just extends the class that you wan't to override, then redefine the function that you want to override in your class. Now, you can use your own class whenever need instead of the vendor class.
For example:
class TokenRepo extends DatabaseTokenRepository{
//Define the functionality here to ovrride
}
Usages:
$token = new TokenRepo();//instead of original DatabaseTokenRepository
Related
I am using Laravel 5.8 and I'm attempting to modify a package class from the Vendor directory. To acheive this, I have created a new class which extends the Vendor class, and I can replace the named functions within it- all working great.
However, the original class 'uses' a class, which I have mimicked in my new class, as follows:
use VendorName\PackageName\OriginalController
// use VendorName\PackageName\SomeClass as StoreRequest; How can I replace this...
use App\Http\Requests\NewRequestClass as StoreRequest; // ... with this..? (not working)
class NewController extends OriginalController {
private function somefunction(StoreRequest $request){ // This doesn't work; it is still using the StoreReqest defined in OriginalController
// ...
}
}
See comments- Is it possible to override this?
Its generally not possible modify/delete class or function. Not without extensions like Classkit. But i am not really a fan of this type of code. But you can Check these questions, which might help:
Redefining PHP class functions on the fly?
Deleting entire PHP Class
Redefining PHP function?
So I override AbstractAdmin because I wanted to add some functions and that all of my admin classes would have it.
It looks like this:
<?php
namespace AdminBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
class BaseAdmin extends AbstractAdmin
{
....
}
As you can see my BaseAdmin extends the default sonata AbstractAdmin class. Everything works fine and all my classes have the custom functions if I extend the new BaseAdmin like: class ProductAdmin extends BaseAdmin
But I noticed one strange thing...
I have a class that uses 'file' type to upload images.
If I extend my admin class with BaseAdmin the form field 'file' is made into a textarea (all other simple textfields are transformed to textarea too for some reason). If I extend the default AbstractAdmin everything works fine..
Do I need to specify my new admin class somewhere in the configs maybe or what is the problem here?
You should show us some more code but from what you already told i would say this is because you override methods in the ProductAdmin class.
In your BaseAdmin class you override AbstractAdmin methods and you call parent::method() but when overriding a method in a class extending BaseAdmin you have to call AbstractAdmin::method() instead of parent::method() as you need to call the grand parent method and not the parent one.
Hope that helps
Is it possible to change the action class Yii2 uses somehow, similar to how you can set the class of many other components within the config file?
I want to extend this class so I can add another member variable to it.
I guess I could just add one to it anyway dynamically, but would prefer to do it in a proper fashion.
Edit: Looking at the list of core application components it isn't listed, so not sure if it's possible?
The proper way to solve this problem is to extend both controller and action classes. If you look at the source code, yii\base\Controller has a createAction method that, if no class action is found, will create an instance of InlineAction.
Since you're extending some kind of controller class every time you make your own controller (class MyController extends Controller), you can just override the original createAction method and in it use your own implementation of the InlineAction class.
It can be done with class map
Yii::$classMap['yii\base\InlineAction'] = '#common/InlineAction.php';
and should be placed into index.php, before the app is launched.
Regardless of its location, common/InlineAction.php should have the same yii\base namespace as the original class.
We are looking to build a system with core classes and the ability to extend these core classes and are looking in to using namespaces.
The problem we are having is working out if we can extend an extended class without extending the class that it extends from
For example, if we have folders and files as below
shared/classes/Entity.php
shared/classes/DatabaseEntity.php - Extends Entity.php
shared/classes/User.php - Extends DatabaseEntity.php
classes/ - Holds classes which extend from the shared classes
If we wanted to create a custom DatabaseEntity class without creating a custom User class , is this possible?
The way I understand this is that the User class will be looking in the shared namespace to extend the DatabaseEntity class but as we have extended the DatabaseEntity class, it needs to look at the top level classes directory
Example of shared/classes/User.php
namespace shared;
class User extends DatabaseEntity {
}
Example of shared/classes/DatabaseEntity.php
namespace shared;
abstract class DatabaseEntity extends Entity {
}
Example of classes/DatabaseEntity.php
namespace custom;
use shared\classes\Entity;
abstract class DatabaseEntity extends Entity {
//Some custom functionality to extend shared/DatabaseEntity
}
So if we didn't want to change the User class to say
use custom/DatabaseEntity
Then is this possible?
Hopefully that makes sense
Thanks in advance for any help
If you don't want to add to User class
use custom/DatabaseEntity
and you want to extend custom/DatabaseEntity
you may just change class declaration from
namespace shared;
class User extends DatabaseEntity {
}
to
namespace shared;
class User extends \custom\DatabaseEntity {
}
if you want to extend \custom\DatabaseEntity.
If it's not want you want to achieve I cannot understand your problem - you ask two questions.
You asked
If we wanted to create a custom DatabaseEntity class without creating
a custom User class , is this possible?
The answer is - yes, you just created it in your example. You created custom DatabaseEntity class without creating custom User class.
But if you want to achieve:
it needs to look at the top level classes directory
you need to tell User class to extend specific class - so you will need to extend using fully qualified class or import namespace using use and creating alias
I don't know if I understand you well, but you want to create CustomDatabaseEntity class that will extend DatabaseEntity and you don't want that CustomDatabaseEntity extends User class.
It's of course possible. You can create as many child classes as you want. As User class is defined that it extend DatabaseEntity class it will even don't know that you created CustomDatabaseEntity
I also think that you are using it a bit wrong. If DatabaseEntity have anything common with database and not with User itself, you should rather create Interface DatabaseEntityInterface, those two DatabaseEntity classes should implement interface
and then in User class you should pass it as constructor argument
class User {
protected $dbi;
public function _construct(DatabaseEntityInterface $dbi) {
$this->dbi = $dbi
}
}
and later you can pass to User class either class for shared folder or the one from classes
I have created a custom class with few method in it. e.g.
class MyClass{
method1 ();
method2();
}
Now I want to create object of this class and use it inside a controller method
class DefaultController{
public function myAction()
{
//here I want to able to create object of MyClass, is it possible?
}
}
Q1. where should I store this class in symfony2 structuere e.g. inside src dir?
Q2. How can I use this class method inside the controller of a bundle?
If you put your class in the src folder, it will be autoloaded, ie: you can simply do:
$foo = new \MyClass();
$foo->method1();
in your Controller.
A good approach would be to put your classes in the Bundle you are likely to use them:
src/YourCompany/YourBundle/MyClass.php
In this way however don't forget to put the namespace declaration on top of your MyClass file:
namespace YourCompany\YourBundle;
class MyClass{
//..
}
You can put your classes on the base folder of your bundle, or use other nested folders to better differentiate a set of classes from each others, for eg:
src/YourCompany/YourBundle/Listener/MyClassListener.php
src/YourCompany/YourBundle/Manager/MyClassManager.php
For more info see the Best practice on Bundles structure of Symfony2