Reputation: 540
Is there any possible way to remove a url rule from the urlManager on the go?
I know I can add a rule via urlManager->addRules()
, but how do I delete them?
Thanks.
EDIT:
My aim is to generate the rules dynamically based on the subdomain.
For example, a subdomain of certain type would have a rule <catalog>/<brand>/<product>
leading to a certain product, while the other would have a different rule. Also, the subdomains are also dynamic, meaning that I can't create any static conditions.
Upvotes: 0
Views: 380
Reputation: 540
Unfortunately, there wasn't a clear way to solve this problem.
The inner rules array _rules
is declared private, and there are no methods for working with it apart from addRules()
. This method, however, only allows you to append the processed rules to the start/end of the _rules
array. You won't be able to delete the rules you've added, too.
The solution I came up with is bad practice, but there was no other way of working around my issue (that I know of).
Declare a method inside CUrlManager
protected function clearInnerRules() {
$this->_rules = array(); // clears the inner rules array
}
In the child class UrlManager
that is used as the manager for the Yii app, declare:
public function reprocessRules() {
Yii::app()->cache->delete(self::CACHE_KEY); // remove anything rule-connected from cache
$this->clearInnerRules();
$this->processRules();
}
Using the onBeginRequest
event, add rules to the placeholders in the array:
// code
Yii::app()->urlManager->rules['rulePlaceholder'] = $newRule;
Yii::app()->urlManager->reprocessRules();
// code
Done. Now I can easily delete the rules or replace them at any moment (provided, this moment is before the request). Sometimes you just gotta do what you gotta do, I guess.
Upvotes: 0
Reputation: 14459
You have all rules here:
Yii::app()->urlManager->rules;
That's an associative array, you can easily loop through it and manipulate it.
Upvotes: 2
Reputation: 3921
It would be great, if you could provide more details about why you need such behavior.
In common, you can extend default CUrlManager
class and add needed functions. After that you can specify this new, let's say, CustomUrlManager
in config.php
as implementation for your urlManager
.
But again - what problem do you try to solve?
Upvotes: 0