Multiple actions for a controller

Permalink
Hi

I am hoping that someone can shed some light on exactly how MVC works in C5 - at the moment I am lost and convinced it doesn't really use MVC at all.

I have a controller, call it feed, this lists some rss feed. I then have an add and edit controller, each with their own view script of the same name. The feed controller is called controller.php and it uses view.php.

Now, I want a new action for the feeds controller, called items. This action will show all the items for a feed, an ID will need to passed along.

How do I set up a new action, called items inside controller.php and point it at a new view for items?

At the moment I can only see how to create controller after controller with view after view, without actually using a controller correctly for multiple actions. It seems like there is one action per controller?

Please can someone help me and tell me this isn't true!

I have attempted to use (the poorly documented) action_*() method, but this seems to be for AJAX and doesn't give me the option to use a view for the "action".

Thanks
Jake

BinaryFold4
View Replies:
xaritas replied on at Permalink Reply
I had this same adventure about 10 days ago, so let me relate what I learned:

1) You're right, it's a partial implementation of MVC.
2) view.php is the only view you get. You'll have to fake it with conditionals in view.php (you can mitigate this somewhat by including an 'elements' file to do the rendering for you), or redirect to a new page with a PageType that does what you want.
3) The action_* thing put me on the wrong path for a few hours too. It is either a red herring or perhaps a relic of prior versions of C5. Any resource appended to the base URI will be invoked as a controller method. E.g., if you POST to /my/silly/resource/createNewFooBaz, then the system will load the Page which is bound to "/my/silly/resource" and look for a method named "createNewFooBaz" on that Page's controller. From there, you do whatever you want, and by default, the view will be instantiated for that page. The bottom of the controller method where I've done my redirects.
ScottC replied on at Permalink Reply 1 Attachment
ScottC
Hello,

xaritas, on point #2 you aren't entirely correct. This isn't at all well documented, but you can have different views for each action.

See my screenshot.

Basically inside of each controller inside of each action you can render a different view, in my case in the "edit" action of the controller that wires up to a view you can call in my case $this->render('/dashboard/page_mapper/manage/edit');

so you don't need a ton of conditionals built on top of $this->controller->getRequestTask() or anything like that.

arrestingdevelopment, i'd recommend setting an array from a controller and rendering a chunk of html foreach via a concrete5 element in the view in a foreach.
xaritas replied on at Permalink Reply
Oh, cool, that will help a bunch. Thanks.
BinaryFold4 replied on at Permalink Reply
BinaryFold4
Thanks ScottC.

This is similar to what I ended up doing, I use
$this->render($viewScript)
to use a different view.

To be honest this has amazed me, this approach, its not MVC at all. It awful and makes a joke of coding for the backend of C5. It's a shame that we need to hack the code together to make it more like MVC, or create an endless stream of files to accommodate it's lack of support for MVC.
ScottC replied on at Permalink Reply
ScottC
Well i wouldn't knock it too bad, it is only one line ;)

I guess it would be nice to have it wire up, but to automatically wire up wouldn't necessarily be ideal.

If you wanted you could probably VERY easily add this kind of behavior to concrete5 if you just submit a pull request for concrete5 on github.

The fact that you can do it in the way we are isn't IMO widely known, especially from looking at some of the other marketplace items, so you're already ahead of the curve.

-Scott
arrestingdevelopment replied on at Permalink Reply
arrestingdevelopment
I'm reaching way out on a limb here (I'm not that strong a programmer and relatively new to C5), but can't you do something like this via functions in your controller and different templates? You could write your "items" function inside the controller.php... and when it's called, it returns it's info by using a display(templates/items.php) or something like that? I know that when you are coding a block into a page type you can specify which template to use to render the block in that same way... so couldn't that work for the controller, too?

If I'm way off base... my bad!

- John