Welcome Guest, Not a member yet? Register   Sign In
Wordpress like url structures
#1

[eluser]stORM[/eluser]
Sorry to ask this I know its ask before in some way (clean urls and such)
I'm reading the documentation up and down but I'm still confused. I wonder how would one create an url structure similar to wordpress.

Let's say we have a site where we have he following links in the navigation:

Home
About
Service
Blog
Contact

My wish would be if you click on those links that you will have an url which looks like:

http://your_name.tld/home
http://your_name.tld/about
http://your_name.tld/service
http://your_name.tld/blog
http://your_name.tld/contact

Lets say you click on the blog link then you would receive the 10 last entries from the database.
Now for each entry inside of the blog category we will have a button where we get the full article when you click on the button we would (I assume) have an url which looks like:

http://your_name.tld/blog/category/your-category/

or

http://your_name.tld/blog/category/date/

or

http://your_name.tld/blog/categroy/id

I tried over and over again, reading about routes, redirections and such but still not able to do that.
I'll ended up with the following:

Model
blog_model.php

Controller
blog_controller.php

View
blog_view.php

Inside of the View I had a function called blog.

This ended up with this url structure when someone click on the navigation section blog

http://your_name.tld/blog/blog/235

or another try I got http://your_name.tld/home/blog/235

Could someone perhaps clarify what to do to have kind of the structure I have mentionend at the beginning also is that possible to build this structure with only one view instead of multiple views for each navigation link?

So lets say you have only one base view (functions are: home,about,service and so on)

Maybe my problem is naming conventions or whatever so could you tell me perhaps:

What names do the views should have what names the function must have and how I route that stuff correctly?

regards
#2

[eluser]Samus[/eluser]
For the category controller, you'd have something like this:

Code:
<?php

class Blog extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('blog');
    }

    function category($id, $limit = null) {

        if (empty($id)) :
            $data['categories'] = $this->blog->get_categories();
// load a view that lists all the categories (can be dynamic or static)
        else :
            $data['bloginfo'] = $this->blog->get_category($id, $limit);
// load a view to display last 10 articles for that category.
        endif;
    }

}

Model will take care of all the smart data:

Code:
<?php

class Blog extends CI_Model {

    function get_categories() {

        $query = $this->db->get('blog_table');

        if ($query->num_rows() > 0) :
            return $query->result;

        else :
            return FALSE;

        endif;
    }

    function get_category($id, $limit = 10) {

//check if $id is a number or string

        if (is_numeric($id)) :

            $query = $this->db->get_where('blog_table', array('cat_id' => $id), $limit);

            if ($query->num_rows() > 0) :
                return $query->result();

            else :
                return FALSE;

            endif;

        else :

            $query = $this->db->get_where('blog_table', array('category' => $id), $limit);

            if ($query->num_rows() > 0) :
                return $query->result();

            else :
                return FALSE;

            endif;

        endif;
    }

}

so if you were to click a link such as example.com/blog/category this should just load a view with a list of categories.

however, if you were to click a link such as example.com/blog/category/4 this should load a view with the last 10 blogs with a 'cat_id' of 4.
or if you were to click a link such as example.com/blog/category/coding this should load a view with the last 10 blogs with a 'category' of 'coding'.

Hope that helps..
#3

[eluser]stORM[/eluser]
[quote author="Samus" date="1333625595"]
so if you were to click a link such as example.com/blog/category this should just load a view with a list of categories.

however, if you were to click a link such as example.com/blog/category/4 this should load a view with the last 10 blogs with a 'cat_id' of 4.
or if you were to click a link such as example.com/blog/category/coding this should load a view with the last 10 blogs with a 'category' of 'coding'.

Hope that helps..[/quote]

Thank you very much!
But it can't be done with just a single controller instead of multiple controllers is that correct?
#4

[eluser]Samus[/eluser]
[quote author="stORM" date="1333627508"][quote author="Samus" date="1333625595"]
so if you were to click a link such as example.com/blog/category this should just load a view with a list of categories.

however, if you were to click a link such as example.com/blog/category/4 this should load a view with the last 10 blogs with a 'cat_id' of 4.
or if you were to click a link such as example.com/blog/category/coding this should load a view with the last 10 blogs with a 'category' of 'coding'.

Hope that helps..[/quote]

Thank you very much!
But it can't be done with just a single controller instead of multiple controllers is that correct?[/quote]

If you mean 'can'. Yes it can be done with just a single controller or multiple, but I don't see why you'd need multiple controllers.
#5

[eluser]stORM[/eluser]
Well I ment the following.
Let's assume later you decide to have kind of a dynamic menu which you load from a database inside a view. So in this situation you can't make multiple controllers since its dynamic. So thats while I basically thought 1 controller for anything would be fine. Same for the view. So all in all you have 3 files which you are able to control everything for your site (1 model, 1 view, 1 controller) thats the goal I would like to archive.

That was also the reason I mentioned Home,About and such links because that would be also needed to handled by the one and only controller. And that confused me because of the link structure it will display later in the browser.
#6

[eluser]gRoberts[/eluser]
I think your getting ever so slightly confused with how CodeIgniter and MVC works.

Out of the box, CodeIgniter only supports www.domain.com/controller/action/param urls, although if you use the "Route" functionality, you can customize the url's to suit your needs.

i.e. this is how I structure my URL's

Code:
$route['blog/category/(:num)/:any'] = 'blog/category/$1'; // www.domain.com/blog/category/1/my-category
$route['blog/(:num)/:any'] = 'blog/article/$1'; // www.domain.com/blog/1/my-article

$route['home'] = 'common/index'; // www.domain.com/home
$route['about'] = 'common/about'; // www.domain.com/about

The reason I do the above, is because it's not only SEO friendly, but I base my URL's on the title of the Article or Category. In the event of you changing the title of the Article or Category using the www.domain.com/category/my-category, any cached links will no longer work.

Using www.domain.com/blog/1/my-article means that I can change my-article to anything and it will always reference article #1.

In addition to this, I use the 404 handler to determine whether the /home or /about is in my database and if it is, I render a dynamic page based on the content stored in the database.

CodeIgniter is extremely flexible Wink

HTH
#7

[eluser]stORM[/eluser]
A little confused? Hrhr as much as possible!
Ok from your example above could you please do the following?

Tell me the name of your default controller and if possible can you tell me also what the function names inside of your controller are for as I said your example above? That would help much thank you!




Theme © iAndrew 2016 - Forum software by © MyBB