Welcome Guest, Not a member yet? Register   Sign In
Controllers decision: Dynamic or static content?
#1

[eluser]Unknown[/eluser]
Hi anyone,

before I start, please note that I'm a student from Germany, so be patient with my language please, but any corrections are very welcome!

CI looks like a great framework to me, but before I start working with it I got to know about one thing:
On almost any website I develop, I have static pages and dynamic pages. The static ones are added and edited by the user like in a CMS, the dynamic ones are just filled with content by the user.
The functionality of the dynamic pages can easily be provided with adding one new Controller for every - let's call it - modul, like a blog, news, or whatever. The URL would then look like this: url/blog/page/1, url/news/article/5, and so on, very nice.
The Question I have is related to these static pages. Let's say we have an homepage and a privacy policy. I want the url look like this: url/home or url/privacy-policy. But this would redirect to a controller named "home" or "private-policy". But the pages, the user create in the backend, are stored in a database and are not available as method or controller. You guys understand what I'm talking about?
I found a way myself to do it the standard way: I created a controller named "site" and other controllers for the different functionalities. The site controller uses the _remap() function to check if the method-trigger is an page-identifier. But then the URIs look like this: url/site/home, url/site/privacy-policy, and so on, an thats not really what i want.
Is there a way of defining a default controller, that takes the uri and checks whether it it's first segment fits to an database item, and if not redirect to the matching "modul"?

Hope anyone understands me :p

Best regards,
Sand*mann
#2

[eluser]tomcode[/eluser]
Hi Sand*mann,

Welcome to CodeIgniter.

Have a look at routing, this should do it for You.

I have an example which works when You use _remap() in Your controllers, for the rest see Routing in the User Guide.

Code:
$route['default_controller'] = "site";
$route['scaffolding_trigger'] = "";

$route['blog/:any'] = "blog/$1";
$route['(:any)'] = "site";
#3

[eluser]Unknown[/eluser]
Fantastic! Works great, thank you! Smile

[Edit]
Wait... it's not:

When you use that:
Code:
$route['default_controller'] = "site";
$route['scaffolding_trigger'] = "";

$route['blog/:any'] = "blog/$1";
$route['(:any)'] = "site";
[/quote]

CI will only redirect to blog, when you call a method from blog. To redirect blog/ to the blog-controller you have to use a short regular expression:

Code:
$route['default_controller'] = "site";
$route['scaffolding_trigger'] = "";

$route['blog(.*)'] = "blog$1";
$route['(:any)'] = "site";
[/quote]
#4

[eluser]n0xie[/eluser]
Tomcode forgot brackets:
Code:
$route['blog/(:any)'] = "blog/$1";
$route['(:any)'] = "site";
That should work.
#5

[eluser]tomcode[/eluser]
@n0xie :

Just tried it : it works also without the brackets. The User Guide uses both.


@Sand*mann:
You're right, I found that this one work's, too (with / without braces):

Code:
// ..

$route['blog'] = "blog";
$route['blog/:any'] = "blog/$1";
$route[':any'] = "welcome";
#6

[eluser]madmaxme[/eluser]
is this not a little stupid ?
so if i have a controller called contact and i go www.example.com/contact it will call welcome and not contact.
so i have to do
$route['contact'] = "contact";

and so for every controller.

is there a way for CI to check is there a controller and if not then go by rule
$route['(:any)'] = "site";
#7

[eluser]madmaxme[/eluser]
is there anyone ?
#8

[eluser]seanloving[/eluser]
[quote author="madmaxme" date="1265769091"]is there anyone ?[/quote]

I'd like to know also. Here is one method that is slightly less bad than using a custom route for each present controller.
#9

[eluser]tomcode[/eluser]
You could extend the Router class, see this thread




Theme © iAndrew 2016 - Forum software by © MyBB