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.

Data Model Questions /

Default action for page_controller


Reply


5 Posts   496 Views

Avatar
dangerdan

Community Member, 3 Posts

11 April 2013 at 4:09am

Edited: 11/04/2013 4:10am

Not 100% this is on the right board but here goes...

I've set up an 'Articles' dataobject to contain articles that are either whitepapers, news articles or blog posts. I've set up an 'ArticlesHome' page for acting as the home page for the various sections of articles (whitepapers/news/blog), and I've set up an action in this page's controller called 'read'.

On my site, visiting site.com/news/read/article-name/ serves up my article fine, but idealy I would like my URL's to simply be /news/article-name/ or /blog/article-name, without the read method/action in there. Seeing as this page class will serve no other purpose than to 'read' articles is there any way I can set my ArticlesHome_Controller to have a default action, which when passed a parameter simply either serves that page, or throws a 404 if the parameter doesnt exist.

I've looked at routes, but couldnt figure out how to apply them to what I want to do above, and I looked at overriding the init() method but I'm guessing that isn't going to solve my problem either.

Thanks

Avatar
Willr

Forum Moderator, 5513 Posts

11 April 2013 at 7:55pm

index() is the default action name for controller. init() is run on every action.

Avatar
dangerdan

Community Member, 3 Posts

11 April 2013 at 8:11pm

okay so obviously i can't use that as that is i still want to use that for the index. how then can i go about achieving what i want to achieve?

Avatar
Willr

Forum Moderator, 5513 Posts

11 April 2013 at 8:17pm

Ah, well I think the way to remove action entirely is to override the handleAction method on your controller (https://github.com/silverstripe/sapphire/blob/3.0/control/Controller.php#L185).

Avatar
dangerdan

Community Member, 3 Posts

12 April 2013 at 2:21am

I tried to over-ride way this morning, though I seemingly hit a wall as the fields in the request object are protected, and couldnt figure out a way around changing them. Is there no other simpler way to achieve what I want? Or is it just not the case with data objects?