I'm new to the MVC model. This is where I started: User authentication system. After questioning, reading and thinking a lot, I was able to write a small framework (don't know if 'framework' is the right word).
I will try to explain everything as simple as possible. I hope you could tell me what I'm doing wrong and what not.
The site structure (folders are bold):
- index.php
- /modules
- /javascript
- /images
- /css
- /applications
- config.php
- Router.class.php
- Registry.class.php
- /model
- /entities
- User.class.php
- Connection.class.php
- /mappers
- UserMapper.class.php
- /services
- AuthService.class.php
- RegistrerService.class.php
- /controller
- HomeController.class.php
- AuthController.class.php
- RegisterController.class.php
- BaseController.class.php (gets extended by all controllers)
- /view
- View.class.php
- /templates
Is this a good site structure? Do you have any tips/suggestions?
Here are some important scripts that show how everything works:
index.php
<?php
require_once('application/config.php');
$registry = new Registry();
$router = new Router($registry);
$view = new View($registry);
$router->setPath('application/controller');
$registry->con = Connection::get();
$registry->router = $router;
$registry->view = $view;
//Load page
$router->load();
?>
<a href="index.php?route=login">Login</a> <a href="index.php?route=register">Register</a>
config.php
error_reporting(E_ALL);
session_start();
require_once('application/Registry.class.php');
require_once('application/Router.class.php');
//Autoload classes
function __autoload($className) {
if (file_exists($class = 'application/model/entities/' . $className . '.class.php')) { //Entities
require_once($class);
}
else if (file_exists($class = 'application/model/mappers/' . $className . '.class.php')) { //Mappers
require_once($class);
}
else if (file_exists($class = 'application/model/services/' . $className . '.class.php')) { //Services
require_once($class);
}
else if (file_exists($class = 'application/controller/' . $className . '.class.php')) { //Controllers
require_once($class);
}
else if (file_exists($class = 'application/view/' . $className . '.class.php')) { //View
require_once($class);
}
}
Registry.class.php
<?php
class Registry {
private $vars = array();
public function __set($index, $value) {
$this->vars[$index] = $value;
}
public function __get($index) {
return $this->vars[$index];
}
}
Router.class.php
This script calls the correct controller (and action). index.php?route=auth/index
will call the index()
function of AuthController.class
.
AuthController.class.php
<?php
class AuthController extends BaseController {
public function index() {
//Create new authenticationService and pass connection variable
$authService = new AuthService($this->registry->con);
if($authService->login('[email protected]', 'testPass')) {
$notice = 'Test logged In';
} else {
$notice = 'Not logged in';
}
$this->registry->view->heading = 'Login';
$this->registry->view->notice = $notice;
//Show the template 'test.php'
$this->registry->view->show('login');
}
}
View.class.php
I will only show the show()
function of this class because it is the most important. It loads the correct template.
public function show($templateName) {
$template = 'application/view/templates/' . $templateName . '.php';
if (!file_exists($template)) {
die('Template not found');
}
//Set template variables
foreach ($this->vars as $key => $value) {
$$key = $value;
}
//Include template file
include($template);
}
login.php
This file is within the templates
folder:
<h1><?php echo $heading; ?></h1>
<?php echo $notice; ?>
Am I doing it right (according to the MVC model)?