Joomla4 Component Dev - subdirectory for views

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Joomla4 Component Dev - subdirectory for views

Post by quasiman » Tue Dec 06, 2022 12:06 am

Is it possible to develop a component with subdirectories for the views? And if so, how is that handled in the real URL such as:
localhost/joomla/index.php?option=com_helloworld&view=tops

Just for an example, say I have a shop that sells clothing, and I want to categorize the different types:
  • Tops
    • Shirts
      • Business
      • Casual
      • Dress
    • Collared
    • Casual
    • Tshirts
  • Pullovers
  • Hoodies
With this logic, I'm hoping I can have a structure like:
  • site/src/Views/tops/shirts/business/casual/HtmlView.php
  • site/src/Views/tops/shirts/business/dress/HtmlView.php
  • site/src/Views/tops/shirts/collared/HtmlView.php
  • site/src/Views/tops/shirts/casual/HtmlView.php
  • site/src/Views/tops/pullovers/HtmlView.php
  • site/src/Views/tops/hoodies/HtmlView.php
  • site/tmpl/Views/tops/shirts/business/casual/default.php
  • site/tmpl/Views/tops/shirts/business/dress/default.php
  • site/tmpl/Views/tops/shirts/collared/default.php
  • site/tmpl/Views/tops/shirts/casual/default.php
  • site/tmpl/Views/tops/pullovers/default.php
  • site/tmpl/Views/tops/hoodies/default.php
I know this would typically be handled by categories, but for my needs I think it would simplify things if I create separate views for each.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2506
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Joomla4 Component Dev - subdirectory for views

Post by SharkyKZ » Tue Dec 06, 2022 6:54 am

Yes, it's possible. But I don't see how this would simplify things. You're mixing content with code which kind of beats the purpose using a CMS. What if your category structure changes? You'll have to rewrite the code.
And if so, how is that handled in the real URL such as:
That's up to you to decide. You'll probably have to write a custom router anyways. Although the default router does support nested views via Joomla\CMS\Component\Router\RouterViewConfiguration::setNestable(). So you can give it a try first. You'll also need to handle the view class and layout file lookup yourself.

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1496
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by ceford » Tue Dec 06, 2022 9:34 am

quasiman wrote:
Tue Dec 06, 2022 12:06 am
Is it possible to develop a component with subdirectories for the views? And if so, how is that handled in the real URL such as:
localhost/joomla/index.php?option=com_helloworld&view=tops
Each 'view' is already a sub-directory. You should read some of the articles on component development from the Joomla Tutorials Project page:

https://docs.joomla.org/JDOC:Joomla_4_Tutorials_Project

Look in the For Developers section. You should aim to use the architecture provided and not build your own.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2506
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Joomla4 Component Dev - subdirectory for views

Post by SharkyKZ » Tue Dec 06, 2022 9:43 am

ceford wrote:
Tue Dec 06, 2022 9:34 am
Look in the For Developers section. You should aim to use the architecture provided and not build your own.
Why? Core MVC is hardly an example of good architecture.

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1496
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by ceford » Tue Dec 06, 2022 10:32 am

SharkyKZ wrote:
Tue Dec 06, 2022 9:43 am
ceford wrote:
Tue Dec 06, 2022 9:34 am
Look in the For Developers section. You should aim to use the architecture provided and not build your own.
Why? Core MVC is hardly an example of good architecture.
I don't understand what you mean by this comment! Is it helpful to the OP?

quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by quasiman » Tue Dec 06, 2022 6:21 pm

Each 'view' is already a sub-directory.
Yes, but only at one level. Organization of the many sub-views becomes harder to maintain if they can't be categorized in more of a parent/children relationship.

User avatar
ceford
Joomla! Ace
Joomla! Ace
Posts: 1496
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by ceford » Tue Dec 06, 2022 8:18 pm

quasiman wrote:
Tue Dec 06, 2022 6:21 pm
Each 'view' is already a sub-directory.
Yes, but only at one level. Organization of the many sub-views becomes harder to maintain if they can't be categorized in more of a parent/children relationship.
There are no Joomla core components that use a tmpl hierarchy. The nearest comparable structure is layouts, for example as seen in com_users.

Each View requires a Controller and Model. Coding that for each product would be a nightmare!

It would be perfectly OK to have a tmpl/default.php file that looks for some input parameter and then includes the appropriate layout. For example:

if ($product = 'tops-shirts-business-casual') { include 'tsbc.php'}
else if ($product = ...) { ...}

You could put your product layout wherever you want. 10 products is maybe not to much work. 1000 products is another matter. Think again!

quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by quasiman » Tue Dec 06, 2022 8:35 pm

10 products is maybe not to much work. 1000 products is another matter. Think again!
I think you're focusing too much on the example I gave, and I apologize if I gave the impression that is exactly what I'm doing. I was just trying to simplify my question with a common illustration.
To clarify, I have a fixed number of pages with each of them in layouts specific to their requirement.
There are no Joomla core components that use a tmpl hierarchy. The nearest comparable structure is layouts, for example as seen in com_users.
This is what I'm talking about:
Image

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2506
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: Joomla4 Component Dev - subdirectory for views

Post by SharkyKZ » Wed Dec 07, 2022 8:00 am

You can add as many layouts to a view as you want. Maybe instead of hardcoding anything, you could make the category layout configurable in backend. Or do you need something more for your use case?

quasiman
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Wed Nov 25, 2009 1:58 am
Location: Oregon
Contact:

Re: Joomla4 Component Dev - subdirectory for views

Post by quasiman » Wed Dec 07, 2022 5:44 pm

Thanks to all for your support; I'm going to figure out a different way to do this. It seems pretty clear from the responses (or lack thereof) that going down this path will just make it more convoluted than it needs to be.


Post Reply

Return to “Joomla! 4.x Coding”