Welcome Guest, Not a member yet? Register   Sign In
url haendling for a cms
#1

[eluser]Ludwig Mair[/eluser]
Hi,

i think about to build a small cms. usually a url calls controller/function/parameter

for the admin area is this functionallity for ok. but for the frontend, the website i have the navigation with urls i have to create urls with clean seo friendly links like

www.mydomain/about.htm
www.mydomain/xysite.htm

i will save the sites in a database. the point is, how can i do this? i would need for each site a controller or can i do this in a different way to just call a main controller to haendle random the selection of site alias names

any idea?

thanks Ludwig
#2

[eluser]LifeSteala[/eluser]
You can create one controller to display the content pages for front end. Using "routes.php" you can specify URLs and route them to this controller.

Example:

Say your controller is "content.php". The URL might be http://www.mydomain.com/content/aboutus/.

In "routes.php", add:

Code:
$route['content/:any'] = "content";

Does that make sense?
#3

[eluser]Ludwig Mair[/eluser]
Hi,
definately, i will try this. thank you for this tip.

Ludwig
#4

[eluser]SitesByJoe[/eluser]
Alternate Solution: I'm assuming your storing your page content and meta data in your database.

I'm also assuming you want to have happy urls like:

Code:
http://mysite.com/pages/the-url-for-my-page

Here's the code from my "pages" controller to accomplish this:

Code:
<?php
class Pages extends Controller {

function Pages()
{
        parent::Controller();
        $this->load->model('Pages_model');
}

function _remap()
{        
        // is there a function name being passed?
        if (! $this->uri->segment(2))
        {
                redirect('');
        }
        else
        {    
                $slug = $this->uri->segment(2);
                $data['pages'] = $this->Pages_model->get_page_by_slug($slug);    
                
                $page = $data['pages']->row();    
                if ($data['pages']->num_rows() > 0)
                {
                        $data['title'] = $page->title;
                        $data['heading'] = $page->heading;
                        $data['description'] = $page->description;
                        $data['keywords'] = $page->keywords;            
                        $data['content_type'] = 'dynamic';        
                        $data['sub_content'] = array();

                        $this->load->view('templates/subpage', $data);
                }
                else
                {
                        echo ' no page found! ';
                }
        }
}

}


/* End of file pages.php */
/* Location: ./system/application/controllers/pages.php */

Hope this helps!
#5

[eluser]Davcon[/eluser]
Hello,

I'm new to CodeIgnite and I'm new to this forum. Thanks for the thread and responses. If I may I'd like to follow up on this. I hope I don't ask anything stupid.

I have carefully looked at your answers. However, there is still something I don't get.

Both solutions above leave you with URLs which are of the form:

www.mysite.com/pages/asdf

Is there any way we can lose that middle pages bit? After all, I'm sure our real aspiration would be to have a CMS which creates URLs which are like:

www.mysite.com/asdf

or

www.mysite.com/asdf.html


Thank you!

-David
#6

[eluser]SitesByJoe[/eluser]
You could use your .htaccess file to add in the "pages", or set "pages" as your default controller. Somewhere you'll need to call the controller though.

I understand your point about having the "pages" in the url and at one point I felt the same way.

Consider thought that if part of the site is a blog, it would have a url like "../blog/index/...." or "blog/detail/2010..." etc.

For myself, I have the "pages" controller handling pages, "portfolio" handling the portfolio etc...

Does this make sense?

On the other hand, if all your doing is making pages (and nothing else) , add the "pages" after the "index.php" in your .htaccess file and set the pages controller as your default controller and use the "_remap" function to route everything through. Done. No "pages", just the url you want.
#7

[eluser]Davcon[/eluser]
Hi. Thanks for your response. I really appreciate it.

I have investigated your solutions and have decided not to go with any of them. Here's what I discovered:

* YOUR FIRST SUGGESTION:
Suggestion: change .htaccess and force folks to go to the pages controller.
Problem: Sometimes I might not want everyone to automatically go to the pages controller. For example, if it's an online shop or an online forum then there may be other types of controllers that I'd want to use.

* YOUR SECOND SUGGESTION:
Suggestion: load pages controller as the default.
Problem: Although it does indeed load the custom pages controller when you go to the homepage, the moment I add 'forward slash anything' to the URL, it freaks out and attempts to load other controllers.

-----------------------------------------

There will be times when I don't want to load the pages controller so I guess that's the big challenge I have.

-----------------------------------------

I can tell you that I've found a solution to this problem. It's a rather crude and simplistic solution but it seems to be working.

* MY SOLUTION
Suggestion: add the name of each custom page to a database table. Then, in the routes script, connect to the database and add whichever names have been added to the route[] array.
Pros: It works. Not only that but it allows me to mix and match any kinds of URLs that I want.
Cons: At the point when the routes script runs, there appears to be no connection to the database, so I have to manually connect to the database on the routes script. This is slightly messy and I'd feel a lot better if I never had to do that.


Thanks again for your help. I owe you one.
#8

[eluser]SitesByJoe[/eluser]
Glad you're finding a way to do what you want. Some in this forum would freak about manually hitting the database... whatever works though, right?

Like I said I was in the same situation and in the end I caved and just left the "pages" in the url.

There are other work-arounds as well. You could check for a ".html" in your route file and of it's there load the pages controller or something like that.

Maybe something like:

Code:
$route['(.*).html'] = 'pages';


Sorry, this may not be exactly right, but give it a try!
#9

[eluser]Davcon[/eluser]
Yeah, that sounds like a good idea. Somebody mentioned it in another thread. However, the challenge with that approach is that I might not always want to run the custom pages controller (for example, if it's an online shop and someone goes to a product page).

Anyway, I'm totally new to all this and really grateful for your help. Although I don't have much to offer this community right now I really hope that some day I'll be able to donate libraries and applications that will help to make other people's lives easier.

Gotta say, when I first investigated frameworks and decided what one to go for, I was rather obsessed with features and performance. However, now I've realised that the best thing any framework can have is a good, friendly and thriving community. That certainly seems to be what's in here so I'm particularly happy and grateful.

I hope that I can ask you and other folks here more questions in the future. If you'd like a link to one of your sites as a way of saying thanks then just ask.
#10

[eluser]SitesByJoe[/eluser]
CodeIgniter rocks! Been using it since 2006 to do pretty much everything. Between the excellent user guide and this forum I've never found a problem I couldn't solve.




Theme © iAndrew 2016 - Forum software by © MyBB