Yii Framework Forum

Can't find Rule/Regular Expression for Custom Route

(DeltaAB) #1

I’m having trouble setting up the rule for the route I want correctly.

I have a SiteController action called “team”, so normally the route is localhost/site/team. I added this rule:

'team' => 'site/team'

So that the url will appear as localhost/team, but there’s also html sections that I navigate to with ’ #text ’ so instead of ‘site/team’, it’s ‘site/team/#text’ but that brings me to localhost/site/team/text# and I want it to also appear as ’ localhost/team/#text

I’ve tried a variety of rules like this:

'team/<id:\#[.*]>' => 'site/team/<id>'

The regular expression should match #text but it gives me a 404 error for localhost/team/#text. I’ve tried other regular expressions but can’t find one that works. Any help is appreciated

(Mehdi Achour) #2

The #anchor section of the url is never transmitted to the server as far as I remember (check your access log). This may be your problem.

Try first by replacing # by “HASH” to validate that # is problematic

(Mehdi Achour) #3

(DeltaAB) #4

Do I replace # with HASH in the rule? Based on that link though, it looks like it has to be grabbed with javascript. I’m wondering if/how to mix grabbing the url with javascript (which I’m not very familiar) with, while also using the rule. Would the value I grab with Javascript replace the part of the rule where the regular expression is?

(Mehdi Achour) #5

The problem is that you can’t use # in your url if you want to manage things using PHP.
Application doing that are usually one-page JavaScript apps, leveraging the # to avoid reloading the page.

I was suggesting using HASH or whatever instead of # first, to validate that your rules are ok. But at the end, you are going to need to use something else instead of # (that will never work for PHP url rules)