Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Looking for a ModelAsController Example


Go to End


5 Posts   2593 Views

Avatar
dalesaurus

Community Member, 283 Posts

15 September 2009 at 3:28am

Howdy! I get the concept of ModelAsController to enable straight DataObjects to return data from URLs REST style without creating a formal *_Controller extends ContentController. Is this logic correct?

If so does anyone have an example of how to do this? I can see that in the Director config there is the base rule that should use the URLSegment to default an Object name to ModelAsController.

ie. http://mysssite.com/SomeObject/asdf/asdf

should trigger some use of

class SomeObject extends DataObject {
    ...
    public function asdf() {
         //Do stuff with $this->URLParams?
    }
}

I can see that the docblock from ModelAsController has been pasted into the wiki: http://doc.silverstripe.com/doku.php?id=modelascontroller

I haven't had any success trying to get a model to return anything. I have tried adding such things to my model as static $URLSegment, static $allowed_actions, and function modelascontrollerInit($c){} with no luck yet. Every attempt to access http://mysssite.com/SomeObject/asdf/asdf returns a 404 Page Not Found.

Thoughts, examples, help anyone? It will be greatly appreciated and I'll be sure to update the wiki with any information provided.

Avatar
Ingo

Forum Moderator, 801 Posts

27 September 2009 at 4:24pm

Why do you want to circumvent the controller? It has its place in the MVC layered architecture, and helps to keep URL-accessible actions separated from your model logic. Its fairly easy to define custom rules through Director in you _config.php:

Director::addRules(
  100, // priority
  array('SomeObject/$Action/$ID' => 'SomeObject_Controller')
);

Avatar
dalesaurus

Community Member, 283 Posts

27 September 2009 at 4:30pm

Edited: 27/09/2009 4:31pm

Honestly it was more of a convenience thing above all else. I have several DataObjects that I would like to have the accessible in a REST manner, mostly for simple AJAX calls. ModelAsController seemed to be in line with this idea, however I was not able to get a DataObject to respond even though everything I read through the code indicates that this was the purpose of developing that pattern.

So, any thoughts on my ramblings or am I off base here? What else would be the practical purpose of ModelAsController if not to circumvent the _Controller class?

Avatar
Ingo

Forum Moderator, 801 Posts

27 September 2009 at 4:39pm

I see ModelAsController more as an intermediate step for instanciating ContentController instances (which in turn point to a DataObject record), and never really used them "controller-less". It can have unintended sideeffects to expose all your public methods through a URL, and as you don't have a controller you cant limit them through Controller::$allowed_actions either. In terms of RESTful URLs, consider using the more well defined paths of RestfulServer for CRUD actions.

Avatar
dalesaurus

Community Member, 283 Posts

27 September 2009 at 5:23pm

Very good points, Ingo. I was falling in line with the "All programmers are lazy" axiom. Using RestfulServer is definitely a better structured option, with all choices laid out.

But I think it should be made clear what ModelAsController is for if it isn't to access Models as a Controller (faults aside). Especially since it doesn't work as advertised. There aren't any good examples on the wiki or complete uses in any modules or SS core code that I've come across.

Its irrelevant at this point, but I was trying to create a write through system for models that had a Session base and MySQL backend. That is why it would have been nice to have all accessors through the Model itself. It would make for bitchin' scalability of heavily accessed DataObjects. :)