Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Give sections an option to route to a controller not just a template #2450

Closed
tjdraper opened this issue Feb 18, 2018 · 10 comments
Labels
enhancement improvements to existing features system administration 💼 features related to system administration

Comments

@tjdraper
Copy link

With Craft 3 we can have a custom module for site specific features, controllers, services, twig extensions, you name it. And it's AAAAAMAZING. However, one thing that's bugging me is this: I've worked on (and am working on currently) sites where every page needs to run through custom controllers to make sure custom variables are set, custom logic is run etc. I've jumped through elaborate hoops to make this work in Craft 2. But in Craft 3, it seems to me there's an opportunity to allow routing to a custom Yii controller as an option and not just a template. If we could specify the controller and method a section should run for the entry's URL in the same way we can specify a Yii controller in a route (for instance dev/locations/entry in the case of a site I'm working on right now, that would be super duper fantastic.

So basically, as a feature request, anywhere we can tell Craft in the CMS to route a URI to a template, also give us the option to route to a controller action.

@tjdraper
Copy link
Author

Meant to say also that the entry that's normally pre-loaded as a variable available in Twig could be an incoming route parameter or attached to the route object in some way.

@brandonkelly
Copy link
Member

brandonkelly commented Feb 19, 2018

I’ve thought about this, but unsure whether enough people would want it, to be worth the added UI complexity.

You can take over elements’ routing though, using the EVENT_SET_ROUTE event.

use craft\base\Element;
use craft\events\SetElementRouteEvent;
use yii\base\Event;

Event::on(Element::class, Element::EVENT_SET_ROUTE, function(SetElementRouteEvent $e) {
    /** @var Element $element */
    $element = $e->sender;

    $params = [
        'element' => $element,
    ];
    $e->route = ['dev/locations/entry', $params];
});

@tjdraper
Copy link
Author

Very helpful, thanks! The only additional argument I'll make in favor of the feature request is perhaps maybe having the route start with a keyword Craft can look for, no additional UI needed. Maybe something like actionRoute/dev/locations/entry. Otherwise, manually doing something like this with that event is probably fine.

tjdraper added a commit to st-mark-reformed/stmarkreformed.com that referenced this issue Feb 19, 2018
@brandonkelly
Copy link
Member

Not a bad idea!

@brandonkelly
Copy link
Member

Played around with it. Issue with supporting the template path being set with something action: is, the column is called “Template”. We’r really need to rename it to “Route” if we support anything besides template paths. And if we rename it to “Route”, it would be pretty awkward that routes defined here default to template paths and you need to specify action: if you want it to go to a controller action; but routes defined in config/routes.php default to controller actions, and if you want it to go to a template, you have to specify that with ['template' => 'template/path']. So I think when we add support for action paths here, we just need both types of routes to explicitly define what they are.

Probably something like this:

screen shot 2018-02-19 at 4 02 03 pm

@carlcs
Copy link
Contributor

carlcs commented Feb 20, 2018

Don't forget about #1489 when thinking about updates to routes. 😉

@brandonkelly
Copy link
Member

Good point @carlcs.

@brandonkelly brandonkelly added the system administration 💼 features related to system administration label Feb 25, 2018
@gijsstegehuis
Copy link

@brandonkelly given your piece of code:
$e->route = ['dev/locations/entry', $params];
Where would $params end up?

I routed it onto a module controller, but the params aren't being set as controller action arguments.

@gijsstegehuis
Copy link

Nevermind that @brandonkelly.

UrlManager will do the trick:
Craft::$app->getUrlManager()->getRouteParams();

@brandonkelly
Copy link
Member

@gijsstegehuis Any parameters that match the names of your controller action arguments will be passed to those. So you could do this:

public function actionEntry(\craft\base\ElementInterface $element)
{
    // $element will be whatever $params['element'] was in the route
}

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
enhancement improvements to existing features system administration 💼 features related to system administration
Projects
None yet
Development

No branches or pull requests

4 participants