? Pending

User tests: Successful: Unsuccessful:

avatar laoneo
laoneo
17 Dec 2021

Summary of Changes

When working on the console, then routing in Joomla doesn't work propperly. Especially the widely used Route::_() code is crashing. This pr adds a function to the console class to get the router object and fills the HTTP_HOST server variable with the live site parameter.

Testing Instructions

  • Enable SEF in the Joomla configuration
  • Create an article
  • Create a blog menu item with the category of the article selected
  • Add the following code to the file /libraries/src/Console/ListUserCommand.php somewhere in the doExecute function:
    $this->ioStyle->success(\JRoute::_('index.php?option=com_content&view=article&id=1&catid=8'));
    Replace the ids 1 and 8 with the ones from your joomla site.
  • In the terminal run php cli/joomla.php user:list

Actual result BEFORE applying this Pull Request

Error in the terminal Call to undefined method Joomla\CMS\Application\ConsoleApplication::getRouter()

Expected result AFTER applying this Pull Request

You see a green OK text with the SEF route to the article.

avatar laoneo laoneo - open - 17 Dec 2021
avatar laoneo laoneo - change - 17 Dec 2021
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 17 Dec 2021
Category Libraries
avatar alikon
alikon - comment - 17 Dec 2021

should fix #34840 i guess... i'll test later

avatar laoneo
laoneo - comment - 17 Dec 2021

@alikon yes it should fix it

avatar laoneo laoneo - change - 17 Dec 2021
The description was changed
avatar laoneo laoneo - edited - 17 Dec 2021
avatar alikon alikon - test_item - 17 Dec 2021 - Tested successfully
avatar alikon
alikon - comment - 17 Dec 2021

I have tested this item successfully on ef15ffe


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36332.

avatar astridx astridx - test_item - 17 Dec 2021 - Tested successfully
avatar astridx
astridx - comment - 17 Dec 2021

I have tested this item successfully on ef15ffe


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36332.

avatar alikon alikon - change - 17 Dec 2021
Status Pending Ready to Commit
avatar alikon
alikon - comment - 17 Dec 2021

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/36332.

avatar Quy Quy - change - 17 Dec 2021
Labels Added: ?
avatar wilsonge
wilsonge - comment - 29 Dec 2021

Sorry this isn't correct (in my opinion) - I accept some of this is somewhat more subjective than other PR's though. There's kinda two parts here so I'm going to address each separately

First of all the getRouter method. Happy to add that to the application so we can use \Joomla\CMS\Router\Route::link. However in terms of the default router even though there is no site router - I think it's wrong to load the site router explicitly as the default. I'd rather it errors out by default if it's called with the wrong data rather than be wrongly relied on.

In terms of the setting of the HTTP_HOST I'm very sure that overloading server side variables is wrong - plus we still end up with potentially bad behaviour here when live_site isn't set (I'm still yet to set that on any of my sites) - and we just end up with that being null and still getting junk URLs out on each of those instances. This needs a proper fix (I'm not 100% sure how) where I guess the application has the ability to inject the base domain into the Uri::getInstance method which would override the HTTP_HOST. Possibly by taking advantage of the WebApplication's https://github.com/joomla-framework/application/blob/2.0-dev/src/AbstractWebApplication.php#L1001-L1004 variables and doing some sort of similar config in the console application so we have consistency over both applications. Open to ideas on the best way forward there - but the idea being we need consistency over both web and console and to treat them as equals rather than bodging around the console.

avatar wilsonge wilsonge - change - 29 Dec 2021
Status Ready to Commit Needs Review
avatar laoneo
laoneo - comment - 30 Dec 2021

First of all the getRouter method. Happy to add that to the application so we can use \Joomla\CMS\Router\Route::link. However in terms of the default router even though there is no site router - I think it's wrong to load the site router explicitly as the default. I'd rather it errors out by default if it's called with the wrong data rather than be wrongly relied on.

I changed the code, so when no name is set that that an exception is thrown. But I left the code in place when a cli router is requested that it uses the site router as this is the case when Route::_() is called.

I'm very sure that overloading server side variables is wrong

There is no other way to get the web site url than from the live site in console applications. But I removed it and now every extension dev has to set it in the console command by itself. Anyway, for me it is important to have routing work in console applications.

avatar wilsonge
wilsonge - comment - 30 Dec 2021

I changed the code, so when no name is set that that an exception is thrown. But I left the code in place when a cli router is requested that it uses the site router as this is the case when Route::_() is called.

But the entire point of Route::_ has always been it's been application specific. That's why we introduced Router::link to start with in 3.7 so we could do cross-application routing.

avatar laoneo
laoneo - comment - 2 Jan 2022

Ok, removed the fallback. All I did was to remove some new code since RTC, So @wilsonge can you consider it now ready to merge?

avatar wilsonge wilsonge - change - 2 Jan 2022
Status Needs Review Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2022-01-02 23:47:27
Closed_By wilsonge
avatar wilsonge wilsonge - close - 2 Jan 2022
avatar wilsonge wilsonge - merge - 2 Jan 2022
avatar wilsonge
wilsonge - comment - 2 Jan 2022

Thanks!

I think for 4.1 (as technically it's a low impact b/c break) we might also want to consider putting getRouter into the \Joomla\CMS\Application\CMSApplicationInterface so typehints work nicer - but this is definitely all we can do for the 4.0-dev branch.

avatar laoneo
laoneo - comment - 3 Jan 2022

Definitely, the whole getRouter stuff needs some love. @bembelimen can you ping me when 4.0-dev is merged into 4.1? So I will do a followup pr.

avatar laoneo
laoneo - comment - 10 Jan 2022

@wilsonge made a 4.1 pr for the interface changes in #36631. But #36630 should be merged first.

Add a Comment

Login with GitHub to post a comment