I recently started programming in PHP using the MVC-pattern, which resulted in one of best choices I made so far, but I read through many articles showing "easy" examples and some of them were quite advanced (based on my opinion).
I started learning the pattern by looking up some code and I ended up here: "php-mvc"-repository by panique
I changed some lines of code and my best approach up to this point is this:
<?php
define( 'DEFAULT_CONTROLLER', 'controller' );
define( 'DEFAULT_METHOD', 'index' );
define( 'CONTROLLERS_DIR', './application/controllers/' );
/**
* Gets <b>$_GET['url']</b>, while assuming it will be used in the sense of <i>MVC</i>,
* it returns information for the controller.
*
* @returns string Returns $_GET['url']
*/
function getUrl()
{
$url = filter_input( INPUT_GET, 'url', FILTER_SANITIZE_URL );
return ( isset( $url ) && !empty( $url ) ? $url : ( defined( 'DEFAULT_CONTROLLER' ) ? DEFAULT_CONTROLLER : null ) );
}
class Application
{
const CONTROLLER_INDEX = 0;
const METHOD_INDEX = 1;
const DEFAULT_CONTROLLER = DEFAULT_CONTROLLER;
const DEFAULT_METHOD = DEFAULT_METHOD;
public $url;
public $controller, $method, $params;
public function __construct()
{
$this->getUrl();
$this->splitUrl();
$this->getController();
$this->getMethod();
$this->getParams();
$this->loadController();
$this->callMethod();
}
/**
* Retrieves the controller url
*/
private function getUrl()
{
$this->url = getUrl();
}
/**
* Splits the url for better access
*/
private function splitUrl()
{
$this->url = explode( '/', rtrim( $this->url, '/' ) );
}
/**
* Gets the controller from $this->url
*/
private function getController()
{
$this->controller = $this->url[self::CONTROLLER_INDEX];
unset( $this->url[self::CONTROLLER_INDEX] );
}
/**
* Gets the method from $this->url
*/
private function getMethod()
{
$this->method = ( isset( $this->url[self::METHOD_INDEX] ) ? $this->url[self::METHOD_INDEX] : self::DEFAULT_METHOD );
unset( $this->url[self::METHOD_INDEX] );
}
/**
* Gets the parameters from $this->url
*/
private function getParams()
{
$this->params = array_values( $this->url );
if ( empty( $this->params ) )
{
$this->params = NULL;
}
unset( $this->url );
}
/**
* Loads the controller
*/
private function loadController()
{
if ( !file_exists( CONTROLLERS_DIR . $this->controller . '.php' ) )
{
$this->controller = self::DEFAULT_CONTROLLER;
}
$this->controller = new $this->controller();
}
/**
* Calls the method
*/
private function callMethod()
{
if ( !method_exists( $this->controller, $this->method ) )
{
$this->method = self::DEFAULT_METHOD;
}
$this->controller->{$this->method}( $this->params );
}
}
Basically, what I wanted to do, is making the code clearer and easily readable, but now I am facing a point, where I got stuck.
- I really wanted to share my approach, because I wanted you to share some thoughts about it.
- Secondly, when I start a project, I always try writing my own little framework, which I require by using
spl_autoload_registerand thenamespace, and I wanted to implement namespaces in the whole project (as it is possible?). - Is there any advice you would like to share (based on experience, ...)? For example: references using the MVC-pattern, things definitely worth reading.