2

so I'm trying to make my own MVC website, I figured the best way to learn PHP better and tackle the big MVC issue is to start a full project. The thing is, I'm stuck at the router, I can't figure out how best to write it, I think this may be a good way at least for the moment but I'm not sure if it is a ..viable one. I was basically thinking of calling the right controller according to the switch case and then if there is a second url param(I've assumed it would be the id of a article for now) to call a method calling a single article and if there isn't a second param to call a method that calls all articles, but I would like to know if this is a bad way of doing it.

function call($controller, $id='') {

switch($controller) {
    case '':
        break;
    case 'pages':
        $controller = new PagesController();
        break;
    case 'articles':
        require_once('controllers/' . $controller . 'Controller.php');
        require_once('models/articles.php');
        $controller = new ArticlesController();
            if(!$id){
                $controller->{ "blog" }();
            }else{
                $controller->{ "article" }($id);
            }
        break;
    default:
        header("HTTP/1.0 404 Not Found");     
        include('/views/404.php');
        exit;       
        break;
}

}

P.S. For now I'm only working with the articles case, that's why the first case only breaks without doing anything and such. Thanks in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
Svetlin Yankulov
  • 73
  • 1
  • 2
  • 8
  • 1
    I think the best way to learn MVC properly is by starting to use already existing frameworks. Codeigniter is dead simple, and very easy to help you understand the basics of MVC :) – Adrian Tombu Dec 30 '15 at 23:04
  • I wanted to truly grasp the idea, because I was having issues seeing the difference between the model and controller, so I found a tutorial that made a basic MVC with very little functionality, but helped show that difference and from that tutorial I decided to take parts and try to make something of my own. – Svetlin Yankulov Dec 30 '15 at 23:07
  • 1
    you probably would benefit from reading this: http://stackoverflow.com/a/19309893/727208 – tereško Jan 01 '16 at 11:12
  • I will read it thoroughly thank you. – Svetlin Yankulov Jan 01 '16 at 18:05

2 Answers2

5

Sure can, see the example below (this is proof of concept and not tested).

switch ($controller) {
    case 'pages':
          break
    case 'articles':
          if ($Apples != $Pears)
          {
              $Tomatoes="Red";
          }
    case "52":
    case "109":
    case "110":
        //do nothing and exit from switch
        break;
    default:
        if ($a == $b)
        {
            $c = "cats!";
        }
        break;
 }

Note that you can also use a switch inside a switch, just like nested if statements.

Steve Byrne
  • 1,320
  • 1
  • 16
  • 29
1

If you have only 2 or 3 option then you can use if else otherwise use nested switch. You can use if in switch also switch in another switch.