Welcome Guest, Not a member yet? Register   Sign In
Hard Routes Problem
#1

[eluser]costicanu[/eluser]
Codeigniter creates url like this: domain_name/controller_name/action_name.
My default controller name is pages. In this pages controller I have an action named page. I want the url to come from the database.
An example: /first-page.html should take the info from /pages/page/$1

I did something with routes:
I used: $route['^(:any)$'] = "pages/page/$1";
But now, everything i'm writting after domain name, for example, domain_name/administration_area/ goes to /pages/page, not to admin area. It's normal, but, all I want is: if the URI is domain_name/page-name-from-database.html take info from /pages/page/page-name-from-database.html

I don't want this to affect all my controllers, should go to pages controller and page action only when URI like this: /page-name-from-database.html
#2

[eluser]Gonzo2[/eluser]
You route should be something like this:

$route['pages/page/(:any)'] = 'pages/page/$1';


Gonzo
#3

[eluser]Gonzo2[/eluser]
Sorry! I realize that you don't want controller and method in the URL just the route with page name. So you need to work like this:

eg: First put all your controllers into regular expression EXCEPT which you do not want to route and let others get routed to your pages/page/page_name.

$route['^(?!admin|members).*'] = "pages/page/$0";

Here I am not allowing admin and members to route, you can change them as per your requirements.

Now in your pages controller and page method capture the uri segment[0].

i.e:
class Pages extends CI_Controller
{
function index()
{
$page_name = $this->uri->segment(0);
}

function page($page_name)
{
$this->load->view($page_name);
}
}


Hope this is what you are looking for and load the url helper if it is not autoload.

Gonzo
#4

[eluser]costicanu[/eluser]
Thanks gonzo2! Your second post regular expression is exactly what I'm looking for. Seems that is time for me to go back to learn them. Thank you!
It rewrites my pages/page/url_from_database/ to /url_from_database
#5

[eluser]Gonzo2[/eluser]
You are Welcome!

That's why we are all here for.

Gonzo




Theme © iAndrew 2016 - Forum software by © MyBB