Welcome Guest, Not a member yet? Register   Sign In
Routes, routes...
#1

[eluser]smilie[/eluser]
Hi all,

I am really having problems gasping the concept of routing in CI.

What I have is:

-application
- controllers
- subdirectory1
- controller1.php
- controller2.php
- subdirectory2
- controller3.php
- controller4.php
-controller5.php

Now, problem is:
When I link some page (a href) I do:

Code:
<a href="subdirectory1/controller1">&lt;?=$this->lang->line('admin');?&gt;</a>

This works fine. But 'somehow' CI remembers that subdirectory, so when I click other link like:

Code:
<a href="controller5">&lt;?=$this->lang->line('reports');?&gt;</a>

Then CI makes automatically "subdirectory1/controller5" from it - but it can not be found (because controller5.php is not in a subdirectory). Even worse, when I click on controller1 (above link) I get subdirectory1/subdirectory1/controller1 Sad

My Routes.php
Code:
$route['default_controller'] = "home";
$route['scaffolding_trigger'] = "";
$route['admim'] = "admin";

So - could someone (preferably with a example) point how do I achieve to use controllers in subdirectories? :-)

Thanks!
Smilie
#2

[eluser]mddd[/eluser]
It is not CI that 'remembers' the directory. It is simply your webbrowser. Don't forget, to the web browser your CI urls are simply urls like any other!
If you are in /subdir/mycontroller/mymethod, and you like to 'othercontroller', it is logical that the browser will try to load 'othercontroller' from the folder where it thinks it is right now!

The solution: start all your links 'from the top', like /subdirectory/controller. Or even better, use the site_url() function in CI to make those links:
Code:
<a href="&lt;?=site_url('subdirectory1/controller1')?&gt;">&lt;?=$this->lang->line('admin');?&gt;</a>
This way, if you ever move the site, the links will follow because they are made based on the $config['base_url'] that you set in your config.php file.
#3

[eluser]smilie[/eluser]
Hi mddd,

Tried - working! :-)
Thanks!

So - $routes[''] is used to call specific Class -> function through URL? Is this only to make URL more user friendly - or are there any functional advantages to it too?

Regards,
Smilie
#4

[eluser]mddd[/eluser]
Routes are used if you need a certain url to start a DIFFERENT controller/method than standard.
Standard means hte basic url of /controller/method/variables. If you do that, you don't need any routes.
But if you wanted to have the url /sitecontent/controller/method/ then you would make a route like this:
Code:
$route['sitecontent/(.*)'] = '$1';
See the Routes page of the CI manual for more info.
#5

[eluser]smilie[/eluser]
Mddd,

Got it!
I believe I was always thinking that URL resembles to views - but it really 'targets' the controller - which on its turn calls view to be presented.

Thanks mddd, you made my life now a bit easier :-)

Cheers,
Smilie
#6

[eluser]mddd[/eluser]
You are exactly right. CI starts a controller and a method in that controller, based on the uri. After that, any view (or more than one view) can be loaded by the controller. Glad to be of help Smile




Theme © iAndrew 2016 - Forum software by © MyBB