Domanda

I have a page set up with an action as is usual, however when I use this page as the home page (url segment is 'home') the action (e.g. http://{BaseHref}/someaction) returns 404. How do I get SilverStripe to recognize home page actions?

class MyPage_Controller extends Page_Controller {

    private static $allowed_actions = array('someaction');

    public function someaction() {
        die('never get here');
    }
}
È stato utile?

Soluzione

in SilverStripe, routing for actions on pages always works the same way:

$baseURL/$URLSegment/Action

this means that if you do /someaction it will not look for an action, instead it will look for a Page with an URLSegment of someaction.

so if you want to call an action on your homepage, you have to include the URLSegment.

that is usually /home/someaction however, I strongly recommend using the $Link method.

$Link('someaction') // will output /home/someaction
// alternatively you can also do $Link/someaction

also note:

  • private static variables like $allowed_actions are cached, you have to flush (visit /?flush=1) to get SilverStripe to reload that cache.
  • $BaseHref is deprecated, please use $baseURL or $absoluteBaseURL instead.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top