Yii Framework Forum: Mod Rewrite With Curlmanagement - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Mod Rewrite With Curlmanagement Rate Topic: -----

#1 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 14 November 2013 - 12:41 PM

Is there any possibility to get the following example:
  • example.com/Itemname instead of example.com/index.php?r=item/index&name=Itemname

So I have to set the CUrlManagement rules dynamically, but how?
My first guess was: try to set UrlManagement rules in the controller or model, but I found this thread: http://www.yiiframew...url-management/ which says, that it isnt possible in yii 1.x
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 01:17 PM

You have found the answer to another question.

In your case all you have to do is

1. create .htaccess file if there's none yet

2. add 'urlFormat'=>'path' to UrlManager config (documentation here)

3. decide how your app should distinguish controller names from Item names.
God is real unless declared as integer
0

#3 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 14 November 2013 - 01:27 PM

But I have to declare the rules for every Itemname anyway?, to create the redirect to index.php?r=item/index?name=Itemname
Sorry, I still dont get it...
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 01:56 PM

Depends on how many items do you have, and what are their names.
Look, you need to tell Yii that in this case it's a item, and in that case it's a controller name.
If you don't, then Yii is not able to tell apart controller name and item name.

There are a lot of ways of doing it, and if you use forum search you'd probably find some examples, because this question is very frequently asked.

My favourite way is using suffixes:
/something.html = item/index?name=something (as param name)
/something = something/index (as controller name)

Other ways are:

- Listing all available controller names in the rule. That is, "if param doesn't look like one of known controller names then it's a param for item/index"

- Modifying default routing rules so that all params starting with, say, 'c_' are controller names. All the rest is params.
That is, "if param equals to c_..., then it's a controller name, otherwise is a param".

- Using virtual 'folders', like '/c/something' = controller, "/something" is a param.

- ... and many more

You'll probably find many good examples here
God is real unless declared as integer
0

#5 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 14 November 2013 - 02:07 PM

I already understand the way how it could work but the Items are user created items, so they could be a few thousand (but they are unique though), I just store them in my DB and want to get a Detailview of these items with example.com/Itemname.
The major problem is, I should declare the rule in my config/main like u said: "/something.html = item/index?name=something (as param name)", but I have no idea how I can declare them automatically
0

#6 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 02:19 PM

Let me give you some examples.

1.
'<controller:site|news|...|user>/<action:\w+>' => '<controller>/<action>', // this rule intercepts all the controllers you have
'<name:\w+>' => 'item/index' // this rule will activate if passed param doesn't look like controller name


Bad thing is you cannot have items named like controllers in this case.

2.
'<name:\w+>\.html' => 'item/index'


In this case all urls like /something.html, /another.html will be processed by item/index.

So, some things to remember:
1. URL rules are just old plain regexps
2. Rules are tested from the top
3. Parser will stop when it finds matching rule.

That's why you should place more specific rules above less specific ones.
God is real unless declared as integer
0

#7 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 14 November 2013 - 03:04 PM

well tried it already before like:
'urlManager'=>array(
      'urlFormat'=>'path',
      'rules'=>array(
      '<name:\w+>\.html' => 'item/create'
    ),
  ),


but I am getting a 404 apache error, thats the problem I had...
0

#8 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 03:41 PM

Do you have .htaccess file?
What URL you're requesting?
God is real unless declared as integer
0

#9 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 14 November 2013 - 04:05 PM

yes, my .htaccess is located in protected/.htaccess and looks like:
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php


Rquested URL:
localhost/project/itemname


Also tested it with the example of the CUrlManager:
'item' => 'item/index'

and it still doesnt work...
0

#10 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 14 November 2013 - 07:53 PM

Are you sure mod_rewrite is on?

Try to remove all you custom rules and access action directly.
In your case, I suppose, it's something liks http://localhost/project/item/index
or you can use some existing controllers, like /contact

If 404 is still there, then check that mod_rewrite is active and AllowOverride for this dir is not None.
God is real unless declared as integer
0

#11 User is offline   spyfx 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 14-March 13

Posted 16 November 2013 - 11:49 AM

alright, it works fine for me.
But got a new problem.

example.com/samsung -> works
example.com/samsung-galaxy-> doesnt work

Error code:
Error 404
Unable to resolve the request "samsung-galaxy".

I guess the problem is because of the hyphen. Do I have to modify the rule somehow?

The rules:
'rules'=>array(
    '<controller:(site|item|login|register|user)>/<action:\w+>' => '<controller>/<action>', 
    '<itemurl:\w+>' => 'item/details' 
)

0

#12 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,689
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 16 November 2013 - 11:54 AM

View Postspyfx, on 16 November 2013 - 11:49 AM, said:

I guess the problem is because of the hyphen. Do I have to modify the rule somehow?


Sure. Just modify it the way it can match all the needed routes.
For example, \w+ ===> [\w\-_]+
God is real unless declared as integer
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users