Yii Framework Forum: Actions by Behavioring - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Actions by Behavioring Is it possible to add actions to a controller via Behaviors? Rate Topic: ***** 1 Votes

#21 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 04 January 2013 - 06:19 AM

Coming back to this rather old topic: I still think, this would be a useful feature, as it would allow you to compose your controllers through behaviors. I don't like the above solutions:

- Action classes: Not always nice as some problems are much easier to handle in single classes
- Base proxy controller: Enforces you to extend from that controller. Since we cant multi-inherit in PHP this is quite limiting if you need some code from your own base controller.

Here's a ticket: https://github.com/y...yii/issues/1922
0

#22 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 534
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 15 July 2013 - 06:48 AM

View PostMike, on 04 January 2013 - 06:19 AM, said:

- Action classes: Not always nice as some problems are much easier to handle in single classes
- Base proxy controller: Enforces you to extend from that controller. Since we cant multi-inherit in PHP this is quite limiting if you need some code from your own base controller.


Why not create a widget that acts as action provider?
0

#23 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 15 July 2013 - 07:12 AM

I'd find it odd to create a Widget if all i actually need is to attach an action to a controller. It's not much different from having separate action classes.

My use case was a Crudable behavior. I've written a base class that implements all CRUD actions in a reusable manner. I tried to convert this into a behavior so that i can attach it to any controller i want. I could enforce all controllers to extend from my CrudController class (that's what i currently do). But i consider this rude: A generic extension should never enforce me to extend my core classes from an extension class.
0

#24 User is offline   Gerhard Liebenberg 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 343
  • Joined: 07-January 12
  • Location:Stillbay - Western Cape - South Africa

Posted 15 July 2013 - 02:43 PM

Would ' include ' be an option?
0

#25 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 534
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 18 July 2013 - 10:54 AM

View PostGerhard Liebenberg, on 15 July 2013 - 02:43 PM, said:

Would ' include ' be an option?


What do you mean? Can you explain a bit more detailed?
0

#26 User is offline   Gerhard Liebenberg 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 343
  • Joined: 07-January 12
  • Location:Stillbay - Western Cape - South Africa

Posted 18 July 2013 - 12:03 PM

Sorry CeBe, I just tried to sound clever about something I know very little about. :rolleyes:

But here is what I do:

I use dynamic actions in a master controller. With dynamic actions, I mean this:

All CRUD actions do more-or-less the same thing and they don't use much of the model's info. For example: for a single table, the Create action would need the model-name and maybe the PK. But that is about all. The rest is done by the model.

So if you store the model-names and PK-names of all your tables in a separate meta-data-table, then you can re-use that single dynamic action to create all your models in all your single tables.

All my controllers are almost empty. They just have enough data to know what meta-data-table records to read. Then, all my controllers extend from a master controller - which contains all the dynamic actions.

All my actions in this master controller can handle 'single table', 'one-to-many', 'many-to-many', and 'tree' structures.

Okay, my master controller has 2000 lines, but if you change one thing, then it works for your entire application. You don't have to make the changes in all your other crud controllers as well. And you have zero duplicate code in your controllers.

But such a huge controller could load unused actions/functions as well. So an even better idea might be to store these dynamic actions in separate files. Then in your controllers, you only import/include the actions you need. But still, if the dynamic action is updated in its own file, then it will be updated for all controllers.

Controllers can obviously have their own unique actions as well. But such actions should be very few, because they will quickly become dynamic actions when you need to re-use them in other controllers as well.

I'm new to yii and mvc, so maybe my reply is not relevant to this topic. I just know that I love dynamic actions, and I hate duplicate code. So any way to 'include' actions will be super.
0

#27 User is offline   Gerhard Liebenberg 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 343
  • Joined: 07-January 12
  • Location:Stillbay - Western Cape - South Africa

Posted 25 July 2013 - 05:01 AM

CeBe

What you do think will be the best way to implement my dynamic actions in Yii 2 - in such a way that I can import/include them into my controllers, only when needed?

Should I put them in widgets, helpers, etc.?
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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