2

I have one model I'd like to use for two different views. This is the only model these two views will require.

I'm subclassing Joomla\CMS\MVC\Controller\BaseController and I'd like to use its display method, so I can leverage the cache. The problem is the display method instantiates a model based on the requested view name.

Without overriding BaseController::getModel or BaseController::getName with some weird conditional logic, I can't see how I can have the display method instantiate two different views, with different names yet instantiate the same model for each one.

Is this done anywhere in the core?

10
  • Clarification: Is there a reason it has to be two views, and not two layouts? The difference between them can be subtle, but I'm referring to things like "category list" and "category blog" -- they use the same model, but they look completely different. What is the driving difference between the two views that means they have to be views and not layouts?
    – Arlen
    Aug 21, 2019 at 19:22
  • There is no layout file. They are raw XML output, with two different structures.
    – Dom
    Aug 21, 2019 at 20:15
  • Also because I want two different menu items to point to those two views.
    – Dom
    Aug 21, 2019 at 21:18
  • It’s in the documentation: docs.joomla.org/Using_multiple_models_in_an_MVC_component
    – Zollie
    Aug 22, 2019 at 7:50
  • i mean, you can see how to set different models to a view in the controller of the view.
    – Zollie
    Aug 22, 2019 at 7:53

1 Answer 1

2

OK, comments are not the easiest place to talk in details (too short, too easy to misinterpret) so I'll post this here. It may be a bit wide-ranging and high-concept at first b/c of the lack of detail I'm starting out with, but bear with me and we can edit out the parts that don't fit the more concrete scenario, as it gets defined, and flesh out the parts you think might apply.

Let's start with outlining the first two possible approaches that come to mind. Maybe, they'll work for you, frankly I don't know because I have about 5% of the information needed to put in details; it'd be too easy to waste a lot of my time writing something that doesn't fit because of an unknown.

Approach the First: Use the type parameter. Joomla uses that while constructing syndication feeds, using type to determine if the output should be the xml structure for RSS feeds or Atom feeds. I'm assuming this is coming from a custom component so in building it, you tell the system the url for it will require a type, and define the field for the menu item to use.

Then, in your view, you check the type parameter and create one of your two XML structures and do whatever it is you're going to need to do to get it out. Your best source of patterns for building something like this is Joomla itself. Joomla's approach to it is triggered by the "format" variable in the request (format=feed), and a lot of it goes through SimplePie, IIRC, so it may or may not suit your taste.

So, you can feel free to use either "format" or "type" to pass the indicator into your view to govern its output.

As for menu item, whether you want to use layout, format, type, or fliedermaus as the label on the variable you send in the request to govern the view's behavior, it's all done the same way. Basic outline for it is at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_a_variable_request_in_the_menu_type and it's fairly complete (the field name they use is id, but it could be anything).

Approach the Second. Use a component and layouts.

(Right up front, let's acknowledge a great deal of confusion in the Joomla world about layouts. They are simply php files that format output. Normally they are used in the html world, because that's typically what Joomla outputs, but they don't have to be. They simply receive data from a view and select bits and pieces to wrap in whatever and ship it on. By using the layout mechanism in Joomla, you can allow for template-based overrides, so it's a bit friendlier, but that all depends upon the ultimate goal.)

Now, if you're defining your own structure you'll want to be bypassing much of what Joomla normally does by default, because that involves HTML. The AJAX component might be the place you want to start reading the code. If not there, you could cast your eyes on Mike Babker's PodcastManager component (https://github.com/BabDev/Podcast-Manager) as an example of something that ships XML formats upon receiving an http request.

Once again, you would use the component's .xml file to set up the request variable you're going to use to control the format in the menu type.

At this point I'd use the concept of layout files from within "normal" Joomla to have the view load the correct layout file to process the data passed into it, transforming it into whatever xml structure you desire. Whether you want to use Joomla's mechanism or just write your own at this point is primarily a matter of taste. Personally, I'd probably tap into Joomla's mechanism (you'll find the basics of it at libraries/src/Layout/FileLayout.php) because of its flexibility. You want to create a third type of XML output? Simply write that layout file, add the variable definition to the component's XML file, and you're done. The rest of your component will already know where to look for it and how to use it. If there were a reason the specific code in Joomla wouldn't work, I'd probably opt to writing my own version of it for this component, simply because of the flexibility.

There's an overview of two different ways to do it with one view. I think either approach covers the bases (at least as far as I can tell at the moment). Let me know if there's a reason they don't, or if there's something missing, and we'll see what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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