1

So I have an entry that has some content displayed in a template.

However ahead of displaying that entry, I want a plugin's action to fire, which pulls some data in from a third party API that will display along with the entry content in the template.

The data from the plugin would need to be available in the template that gets loaded.

What's the best approach to do this?

Here's my routes.php file:

return array(

    'book' => array(
        'action' => 'micros/availability/search'
    )

);

And my plugin:

<?php
namespace Craft;

class Micros_AvailabilityController extends BaseController
{

    protected $allowAnonymous = true;

    public function actionSearch()
    {
        Craft::dd('Micros_AvailabilityController->actionSearch()');
    }
}

I have an Entry with a slug of 'book' and that loads a template from the templates directory, so what I'm seeing right now is that template, and I don't get the action method running at all.

What I want to do is do something in my action and attach the data to the request and return to whatever template the entry is using.

  • I don't think it is? I want it to run on a particular path (for example an entry slug like 'my-page') let the plugin do it's thing and then allow the execution to continue (i.e. load the template for the 'my-page' entry along with the data the plugin provided along the way. – Russ Back Mar 20 '15 at 18:50
  • Unless I'm missing something, seems exactly the same to me. At the end of your action method, you'd just render the template yourself with any new variables you need: i.d. $this->renderTemplate('template/path', $variables); – Brad Bell Mar 20 '15 at 18:54
  • OK, perhaps I'm misunderstanding it, or got it wrong. So I want to hook onto 'book' and run an action, however the action isn't getting run (it's fine if I go direct to actions/micros/availability/search: public function registerSiteRoutes() { return array( 'book' => array('action' => 'micros/availability/search') ); } – Russ Back Mar 20 '15 at 18:58
  • 1
    If it's not on a publicly accessible site, sure. support@buildwithcraft.com – Brad Bell Mar 20 '15 at 20:48
1

It's more clear to me now that I've seen the database and extra description from the ticket.

The issue is that you've got an entry that already has has the URI of book.

Normally the code you're trying to use would work, but in resolving a route Craft matches any entry/category requests (Step 4) before it matches any dynamic routes (Step 5).

When you're testing the action URL directly in the browser that works because it hits step 2.

One way to pull off what you're looking for is to give your plugin a template hook. Then from the template that loads when book is hit, you can do something like:

{% if craft.request.getSegment(1) == 'book' %}
    {% hook "myAwesomeHook" %}
{% endif %}
| improve this answer | |
  • So with this approach my plugin's init would call my service method, rather than a controller. Or should I be somehow having my init method calling the controller? – Russ Back Apr 14 '15 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.