Welcome Guest, Not a member yet? Register   Sign In
routing issue?
#1

[eluser]andieevans[/eluser]
Hi

I am new to codeigniter and this forum.

I have a simple controller, called public_site.php

in the routes.php I have set the default_controller to public_site

the controller has 2 methods

index

about

1: if I browse to 'www.myurl.com/ci' the index page displays fine

2: if I browse to 'www.myurl.com/ci/index.php/about' i get a 404 page not found

3: however if i browse to 'www.myurl.com/ci/index.php/public_site/about' it displays the page


also I am having problems with the location of the css.. in scenario 1 above the page displays with css correct, however with scenario 3, the page displays with no css... how do you keep track of the css location if you are using a template header and footer?

Thanks for listening

andieevans
#2

[eluser]Ninjabear[/eluser]
Yes this is because CI uris work as follows:

domain.com/index.php/controller/method/id

You left out the controller so you got a 404 error.

It should be /public_site/about
Or /public_site (which would give you the index method.

Just /ci uses the default controller as set in your routes file. $route['default_controller'] = "main";

Also, the css goes in the root of your install so inside the ci folder I suppose alongside the application folder in a folder called /css for example.
#3

[eluser]andieevans[/eluser]
Hey Ninjabear, thanks for the reply

What I am having problems with is, i set the default controller to 'public_site'

$route['default_controller'] = 'public_site';

I thought that changing the default controller meant I don't need to reference it in the URL any longer, and that index.php/about would automatically redirect to public_site/about

I have got around the issue by putting a route for each page I need like this

$route['about'] = 'public_site/about';
$route['faq'] = 'public_site/faq';
$route['news'] = 'public_site/news';

Is this the way to do it?

I also used the base_url() helper to obtain the path to my css..

#4

[eluser]Ninjabear[/eluser]
[quote author="andieevans" date="1332500657"]Hey Ninjabear, thanks for the reply

What I am having problems with is, i set the default controller to 'public_site'

$route['default_controller'] = 'public_site';

I thought that changing the default controller meant I don't need to reference it in the URL any longer, and that index.php/about would automatically redirect to public_site/about

[/quote]

No it doesn't work that way. The default controller is just useful so you can change where your application begins processing.

[quote author="andieevans" date="1332500657"]

I have got around the issue by putting a route for each page I need like this

$route['about'] = 'public_site/about';
$route['faq'] = 'public_site/faq';
$route['news'] = 'public_site/news';

Is this the way to do it?

[/quote]

You can do it this way just fine, but be aware that whenever you create routes like this you are forcing yourself to create new routes every time. This is because you've broken the controller/method/id format. So if you route, tasks/:any/:any/ to tasks/mymethod/$1/$2/ then you end up with no ability to create a uri like tasks/mymethod/10/. The moment you use the uri tasks/edit_task/10 you end up at mymethod again, just warning you you'll have to keep making new routes in some cases.

There's no need to shorten the urls that much really, I would stick to the defaults and just use public_site/about and public_site/news as users probably will be happy with that.
#5

[eluser]andieevans[/eluser]
Hey ninjabear, thanks again for the headsup... I think I will stop with all this routes stuff...

so if I have a url like this:

index.php/public_site/news/1

how do I access the value 1?

#6

[eluser]Ninjabear[/eluser]
[quote author="andieevans" date="1332504066"]Hey ninjabear, thanks again for the headsup... I think I will stop with all this routes stuff...

so if I have a url like this:

index.php/public_site/news/1

how do I access the value 1?

[/quote]

You would use something like: $this->uri->segment(3) or there is rsegment() to use the routed segment number.

You can also do something like:

Code:
function mymethod($id)
{
  echo $id;
  echo $this->uri->segment(3); // Does same thing.
}

I recommend looking up the Shawn McCool tutorials, those will teach you loads of stuff.
#7

[eluser]andieevans[/eluser]
geat stuff, thanks so much... I will check out shawn mccool too!
#8

[eluser]Ninjabear[/eluser]
[quote author="andieevans" date="1332508021"]geat stuff, thanks so much... I will check out shawn mccool too!
[/quote]

You're welcome.




Theme © iAndrew 2016 - Forum software by © MyBB