Search code examples
phpcodeigniterurl-parameterscodeigniter-routing

How do I use MVC to load pages without using parameters?


I currently have a website where I have not implemented MVC, but am testing the waters to see if it is something I want to do. I have looked around on the net and haven't found a clear answer to my question. I sort of understand the concept of MVC; however, I am wondering how pages made by a user are handled with MVC.

Currently, I load pages mainly based on parameters. Users can add their own quotes and these are inserted into the database. Then to display the quotes from an author they've added, a user will go to, for example, mysite.com/author.php?quotee=Randy+Pausch.

Ideally, I want to transform my site so that the URLs look like mysite.com/authors/Randy-Rausch or something similar.

This is what I don't understand about MVC. Do I need to somehow create a view for each author, and if so, how would I do this? Or is the last part of the URL actually a parameter even though it doesn't use the "?quotee=Some+Author" syntax? I feel like there is a fundamental piece of MVC I'm not understanding.

If anyone could provide me with a good approach or a good tutorial to my problem I would really appreciate it. I've seen tutorials on static pages, but would like a tutorial based on pages that can be created by users of a site.

Thanks for any help!


Solution

  • You create "controllers" that handle all your application logic. In this case it would look at what authors you are requesting, and decide on which "view" to show. Your controller can look up database information for each author through a "model". Your "controller" can then send this author information to your "view" which will display it.

    You only need one "view" to display an author page. It would be something like this:

    Author name: <?php echo $author_name?>
    Book title: <?php echo $book_title?>
    Category: <?php echo $category?>
    

    Then in your controller you would have something like

    $data['author_name'] = $this->your_model->get('author_name');
    $data['book_title'] = $this->your_model->get('book_title');
    $data['category'] = $this->your_model->get('category');
    $this->load->view('authors_page', $data);
    

    Obviously there is more too it than that - but hopefully that gives you a rough idea.