Welcome Guest, Not a member yet? Register   Sign In
Dynamic Routes Rules
#1

[eluser]serhat[/eluser]
In my new cms i have page system like
domain.com/contact
domain.com/....
i'm defining them for per page like this
$route['contact'] = "blog/page/contact";

Is it possible to do this dynamic
For example i added a page named ci. I don't want to write this to route by hand everytime.
$route['ci'] = "blog/page/ci";
In routes file can it read from mysql and with if condition it put to route file auotmaticly?
Thanks for help
#2

[eluser]bigtony[/eluser]
You could try this:
Code:
$route['(:any)'] = "blog/page/$1";
It's explained in the [url="http://ellislab.com/codeigniter/user-guide/general/routing.html"]User Guide[/url] ;-)
#3

[eluser]serhat[/eluser]
I know but my (:any) is already filled with something else so i have to use it with another way
#4

[eluser]Dam1an[/eluser]
What does :any to map anyway?
You could always make that function do a lookup for segment(3) as a slug in the CMS, and if that returns no results, carry on with whatever that function was doing before, otherwise, do the CMS thing
#5

[eluser]doob[/eluser]
Code:
$pages = array('ci', 'home', 'foo', 'bar'); //get this from your DB

foreach($pages as $page)
{
    $route[$page] = "blog/page/$page";
}

Maybe?
#6

[eluser]serhat[/eluser]
In :any i have posts of my website. so when i want to use it as page. I points another place.
I don't want url like asd.com/page/my-page

So i think the best solution(if it's working on routes.php) grabbing them from database.
Else
I think i can check whether that uri->segment is page or normal post
That's another way.
[quote author="Dam1an" date="1249936292"]What does :any to map anyway?
You could always make that function do a lookup for segment(3) as a slug in the CMS, and if that returns no results, carry on with whatever that function was doing before, otherwise, do the CMS thing[/quote]
#7

[eluser]Jondolar[/eluser]
Create a different method in your blog controller. Use the "page" method/function for your "pages". Use the other function (such as "posts") for your "normal posts". Now, in the posts() function you can look at segment 3 of the uri to determine which post to use.

You won't need to use any custom routes to do it this way.
#8

[eluser]Kepler[/eluser]
If I am understanding correctly you want normal page URLs to look like:

www.example.com/contact

And blog posts to look like:

www.example.com/blog/post-name

I would use mod_rewrite (if you are running under apache) to rewrite the URLS for you. In your .htaccess file:

Code:
<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !blog.*
RewriteRule ^(.*)$ blog/page/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !blog/page/.*
RewriteCond %{REQUEST_URI} !blog/post/.*
RewriteRule ^blog/(.*)$ blog/post/$1

</IfModule>

Now www.example.com/contact => www.example.com/blog/page/contact

and www.example.com/blog/post-name => www.example.com/blog/post/post-name

Hopefully I understood your issue correctly.
#9

[eluser]Donny Kurnia[/eluser]
Hi,

I might have same problems and wonder if this can be solved using mod_rewrite.

I want username in the first segment, like this:
http://site/username

But I also need to access existing controller:
http://site/admin
http://site/welcome
http://site/u/get_by_name/username <-- this is the real handler for case where username as first segment above

My question is, how I can write the mod_rewrite rule?

I tried using RewriteCond, but ended all url redirected to:
http://site/u/get_by_name/

So I cannot access:
http://site/admin

Please help.
#10

[eluser]Donny Kurnia[/eluser]
I'd like to answer my own question. I just have it resolved.

This is the content of my .htaccess file
Code:
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /

  RewriteCond %{REQUEST_URI} ^/system.*
  RewriteRule ^(.*)$ index.php?/$1 [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !^/(admin|u|other_controller)/.*
  RewriteRule ^(.*)$ index.php?/u/get_by_name/$1 [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

With this setup, if I go to: http://site/username
the page I see actually will be:
http://site/u/get_by_name/username

I can still go to other controller page, such as:
http://site/admin/index

I hope this will help other that have same problems.

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB