Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] protected $_router instead of private #31251

Closed
wants to merge 1 commit into from

Conversation

sakiss
Copy link
Contributor

@sakiss sakiss commented Oct 27, 2020

Summary of Changes

Set the $_router variable as protected instead of private in the class: Joomla\CMS\Router\Route
By doing that we can extend the Route class and define our own Router in that.

This way we can provide additional functionalities in our Router.

Testing Instructions

It can only be tested by extending the Joomla\CMS\Router\Route and defining our own Router

Actual result BEFORE applying this Pull Request

No Router can be defined in sub-classes.

Expected result AFTER applying this Pull Request

You can define your own Router in sub-classes.

Documentation Changes Required

DKN

@sakiss sakiss changed the title protected $_router [4.0] protected $_router instead of private Oct 27, 2020
@HLeithner
Copy link
Member

Can you give me an example what you want to achieve?

@sakiss
Copy link
Contributor Author

sakiss commented Oct 27, 2020

@HLeithner In my case i want to override the Router::createUri, in order to encode the special characters included in the url's query vars.

But i think that can be used in a more broad way e.g. set validation and escape rules.

@HLeithner
Copy link
Member

Maybe I'm wrong but wouldn't it be easier to add a preprocessor?

@sakiss
Copy link
Contributor Author

sakiss commented Oct 27, 2020

@HLeithner Thanks for the suggestion.
Any idea on how this can be implemented?

@HLeithner
Copy link
Member

Create a plugin and add a buildRule in the onAfterInitialise event. somehing like this:

class plgSystemMyRouterExtgension extends JPlugin {

public function onAfterInitialise() {
  $app = JFactory::getApplication();
  if ($app->isSite()) {
    $router = $app->getRouter();
    // attach build rules for translation on SEF
    $router->attachBuildRule(array($this, 'buildRule'), JRouter::PROCESS_BEFORE);
  }
}

public function buildRule(&$router, &$uri) {
  /* modify $uri here */
}

}

@sakiss
Copy link
Contributor Author

sakiss commented Oct 29, 2020

@HLeithner Thank you!
The actual problem is in the Uri class, hence it requires more than adding rules.

In any case i think that providing the ability to set our own Router could add extra flexibility.

@HLeithner
Copy link
Member

Can you send me the code you want to use in your router if this variable is not private?

@sakiss
Copy link
Contributor Author

sakiss commented Oct 29, 2020

I am overwriting the Router::createUri by calling my own Uri class within that fn.

@dgrammatiko
Copy link
Contributor

@sakiss if you're essentially overriding the Uri class probably you should consider doing it like the example here: #19842

@sakiss
Copy link
Contributor Author

sakiss commented Oct 30, 2020

@dgrammatiko Valid. The only reason i don't like that, is that is sets an object in the container which will be used globally. I prefer having something with limited scope. But i understand the point that the same class should be used on input and output (build/parse).

@sakiss sakiss closed this Nov 6, 2020
@sakiss sakiss deleted the protected_router branch November 6, 2020 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants