Skip to main content
deleted 26 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Is my PHP MVC structure correctframework

I am kind of new to MVC, I've read lots of tutorials online but each keep using different approaches so I decided to create mine. Below is what my code looks like.

controller would be 'accounts'
action would be 'add'
parameter ($ulr_segment[0]) would be 'student'
  • controller would be 'accounts'
  • action would be 'add'
  • parameter ($url_segment[0]) would be 'student'

Is this really MVC or have I totally lost it.?

Secondly, my controller uses conditional statement based on parameters supplied after the controller and action part of the url to call different views, and I've read somewhere that having controller decide what view to load is bad and MVC would not have been structured properly. IsAre there any changes I'll need to be making to my code above.?

Is my MVC structure correct

I am kind of new to MVC, I've read lots of tutorials online but each keep using different approaches so I decided to create mine. Below is what my code looks like

controller would be 'accounts'
action would be 'add'
parameter ($ulr_segment[0]) would be 'student'

Is this really MVC or have I totally lost it.

Secondly, my controller uses conditional statement based on parameters supplied after the controller and action part of the url to call different views, and I've read somewhere that having controller decide what view to load is bad and MVC would not have been structured properly. Is there any changes I'll need to be making to my code above.

PHP MVC framework

I am kind of new to MVC, I've read lots of tutorials online but each keep using different approaches so I decided to create mine. Below is what my code looks like.

  • controller would be 'accounts'
  • action would be 'add'
  • parameter ($url_segment[0]) would be 'student'

Is this really MVC?

Secondly, my controller uses conditional statement based on parameters supplied after the controller and action part of the url to call different views, and I've read somewhere that having controller decide what view to load is bad and MVC would not have been structured properly. Are there any changes I'll need to be making to my code above?

Source Link

Is my MVC structure correct

I am kind of new to MVC, I've read lots of tutorials online but each keep using different approaches so I decided to create mine. Below is what my code looks like

index

$url_segments = explode('/', $url);
$controller = !empty($url_segments[0]) ? $url_segments[0] : 'home';
array_shift($url_segments); // removes the controller name from array
$action = isset($url_segments[0]) && !empty($url_segments[0]) ? $url_segments[0] : 'index';
array_shift($url_segments); // removes the action name from array
    
$controller = ucfirst($controller);
$controller = new $controller($url_segments);
$controller->$action();

controller

class Controller
{
   protected $model;
   protected $view;

   public function __construct() {
        $this->view = new View();
   }
}

model

class Model 
{
    private $connection;

    public function __construct() {
        require_once DOCUMENT_ROOT . '/resources/connection.php';

        $db = new DatabaseFactory();
        $this->connection = $db->getFactory()->getConnection('localhost', 'db_un', 'db_pw', 'db_nm');
    }

    public function getConnection() {
        return $this->connection;
    }
}

view

class View
{
    private $file;
    private $data;

    public function output($file, $data = null, $content_only = false) {
        if (isset($data) && is_array($data)) {
            foreach ($data as $key=>$value) {
                ${$key} = $data[$key];
            }
        }

        ($content_only == false) && require_once TEMPLATES_PATH . '/header.php';
        require_once $file;
        ($content_only == false) && require_once TEMPLATES_PATH . '/footer.php';
    }
} 

For example if the url example/accounts/add/student was visited,

controller would be 'accounts'
action would be 'add'
parameter ($ulr_segment[0]) would be 'student'

Accounts class

class Accounts {
    public function __construct() {
        parent::__construct();
    }

    public function add() {
        if (!isset($this->url_segments[0])) { 
            // url entered was: example.com/add so query type
            
            // rendering view
            $this->view->output(VIEW . 'query_account_type_to_add.php');
        } else if (isset($this->url_segments[0]) && $this->url_segments[0] == 'student') {
            // code to add student account

            // rendering view
            $this->view->output(VIEW . 'add_account.php');
        }
    }
}

Is this really MVC or have I totally lost it.

Secondly, my controller uses conditional statement based on parameters supplied after the controller and action part of the url to call different views, and I've read somewhere that having controller decide what view to load is bad and MVC would not have been structured properly. Is there any changes I'll need to be making to my code above.