Yii Framework Forum: How to extend an extension object? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to extend an extension object?

#1 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

  Posted 22 May 2017 - 02:31 AM

If there is an extension that defines a model class that gets used by controllers and view files within the extension, how could I extend this class with Yii means in my application?

E.g. there could be an extension 'product-management' that defines a model (ActiveRecord) 'Product' that has some basic features. There is a controller with view files. Now in my application I'd like to enrich (that uses the extension) this class with new properties and validation rules, relations and maybe some further functions.

I see here different challenges: Where should the object creation take place? Can I enrich the view files? Can I use the controller in the extension if it does not need to be changed?

Is a factory the right pattern? Could I use behaviours anyhow? Should I override everything in the extension? What would be a sound solution for this issue?

And does the extension need to be prepared for such things?

I hope someone has any hints... Thanks!
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 22 May 2017 - 06:34 PM

Quote

Where should the object creation take place? Can I enrich the view files? Can I use the controller in the extension if it does not need to be changed?


Depends on the extension.

Quote

And does the extension need to be prepared for such things?


Yes. It should be flexible in order to make all the above possible.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

Posted 24 May 2017 - 01:52 AM

View Postsamdark, on 22 May 2017 - 06:34 PM, said:

Depends on the extension.

Yes. It should be flexible in order to make all the above possible.


Thank you for the feedback!

If the extension has MVC and CRUD code that was created with gii, how far can it be used with regard to overriding? E.g. can I change the view files for the controller in the extension so that view files in the application gets used? Could I change the Model with which the controller deals?

How could I prepare an extension for such a task? Which steps are required for what?

I would think that this is an intersting topic that Yii could support as far as possible (if it not already does): An extension provides some business models and logic that can be adopted to ones own needs easily and flexibly.

I know there are extensions that deal with user management. That is probably a good example for such things: In an application user objects should be extended to have and do more things. Do you know if those extension provide such possibilties? Or are there any other examples?
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 24 May 2017 - 06:26 AM

Gii doesn't create controller code that could be reused because there's no sense in re-using CRUDs in most cases. They're evolving and each one is moving into different direction.

So overall you'll need to put together a list of what you'd like to be able to change and implement your extension accordingly.

Quote

can I change the view files for the controller in the extension so that view files in the application gets used?


I'd look at themes.

Quote

Could I change the Model with which the controller deals?


If you build your controller the way that it could deal with any AR model — yes.

Quote

I know there are extensions that deal with user management. That is probably a good example for such things: In an application user objects should be extended to have and do more things. Do you know if those extension provide such possibilties? Or are there any other examples?


User extensions I've seen are mostly done in not really flexible way. Ideally your module should define an interface with methods it needs. This interface should be implemented by user model in the application.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#5 User is offline   robsch 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 14-February 13
  • Location:Austria

Posted 25 May 2017 - 05:41 AM

View Postsamdark, on 24 May 2017 - 06:26 AM, said:

If you build your controller the way that it could deal with any AR model — yes.


Last question: how could I do this and can I benefit from using Yii? Can you provide an example (or a link) with some lines of code?
0

#6 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 26 May 2017 - 03:44 AM

Check how REST API for AR is implemented. That should give you an idea.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users