For a Joomla 4 component I just cannot figure out how to successfully forward or call a Controller function from within a custom Router.
This is the Controller - it basically loads an image:
<?php
namespace Mycompany\Component\Myproject\Site\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Image\Image;
use Joomla\CMS\MVC\Controller\BaseController;
class ImageController extends BaseController
{
/**
*
*/
public function show()
{
$id = $app->input->get('id', \null, 'string');
// ... logic to load the image
}
}
Using this URL the image shows up:
/index.php?option=com_myproject&task=image.show&id=123
To support SEF URLs inside the component the URL gets called like this:
/img/product_12312.jpg
In Joomla 3 I did this by configure mod_rewrite in the .htaccess. Now this should get done by the Router:
<?php
namespace Mycompany\Component\Myproject\Site\Service;
\defined('_JEXEC') or die;
use Joomla\CMS\Component\Router\RouterBase;
class Router extends RouterBase
{
public function parse(&$segments)
{
$total = \count($segments);
$vars = [];
switch ($segments[0]) {
case ('img'):
$vars['id'] = str_replace('.jpg', '', explode('_', $segments[1])[1]);
$vars['task'] = 'image.show';
break;
}
// print_r($vars); // seems to work so far
return $vars;
}
// .... other code
}
The parse() function correctly splits the URL into the $vars array().
When I call the SEF image URL in the browser, I get a 404 error. - What am I missing to internally call the show() function of the ImageController class?