1. Go to this page and download the library: Download php-mvc-project/php-mvc library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php-mvc-project / php-mvc example snippets
// use aoutoload (recommended)
PhpMvc\AppBuilder;
// be sure to specify the namespace of your application
AppBuilder::useNamespace('RootNamespaceOfYourApp');
// use session if you need
AppBuilder::useSession();
// routes
AppBuilder::routes(function($routes) {
// skip all the paths that point to the folder /content
$routes->ignore('content/{*file}');
// default route
$routes->add('default', '{controller=Home}/{action=index}/{id?}');
});
// build
AppBuilder::build();
// make sure you add the Controllers child name to the root namespace of your application
namespace RootNamespaceOfYourApp\Controllers;
// import the base class of the controller
use PhpMvc\Controller;
// expand your class with the base controller class
class HomeController extends Controller {
public function index() {
// use the content function to return text content:
return $this->content('Hello, world!');
// create to the ./view/home/index.php
// and use view function to return this view:
// return $this->view();
}
}
namespace RootNamespaceOfYourApp\Controllers;
use PhpMvc\Controller;
use PhpMvc\Model;
class AccountController extends Controller {
public function __construct() {
Model::set('join', 'join');
Model::email', 'Email');
Model::display('join', 'password', 'Password');
Model::display('join', 'confirmPassword', 'Confirm password');
Model::compare('join', 'confirmPassword', 'password');
Model::validation('join', 'email', function($value) {
return filter_var($value, \FILTER_VALIDATE_EMAIL);
});
}
public function join($model) {
if (!isset($model)) {
$model = new ModelForExample();
}
return $this->view($model);
}
}
use PhpMvc\Html;
use PhpMvc\View;
// model intance (for the convenience of working with the IDE)
$model = new RootNamespaceOfYourApp\Models\AnyClass();
// set layout
View::setLayout('layout.php');
// set title
View::setTitle('Test title');
// iject model (from action)
View::injectModel($model);
class HomeController extends PhpMvc\Controller {
public function index() {
return $this->view();
}
public function hello() {
return $this->view();
}
public function other() {
return $this->view();
}
}
class AnyController extends PhpMvc\Controller {
public function example() {
$view = new ViewResult();
// set the name of an existing view file
$view->viewFile = '~/view/abc/filename.php';
// set the title
$view->title = 'The view is created programmatically';
// set the layout file name
$view->layout = 'layout.php';
// create model for view
$model = new Example();
$model->title = 'Hello, world!';
$model->text = 'The model contains text.';
$model->number = 42;
// set model to the view
$view->model = $model;
// return view
return $view;
}
}
class TestController extends Controller {
public function __construct() {
// add filter TestFilter to all actions of the controller
Filter::add('TestFilter');
// add filter ExceptionToJson to error action
Filter::add('error', 'ExceptionToJson');
}
public function index() {
return $this->content('Hello, world!');
}
public function other() {
return $this->view('~/views/home/other.php');
}
public function anyname() {
return $this->view();
}
public function error() {
throw new \Exception('Any error here!');
}
}
namespace RootNamespaceOfYourApp\Filters;
use PhpMvc\ActionFilter;
use PhpMvc\ContentResult;
class TestFilter extends ActionFilter {
// action executed handler
public function actionExecuted($actionExecutedContext) {
// check exception result
if (($ex = $actionExecutedContext->getException()) === null) {
// no exception, replace result
$actionExecutedContext->setResult(new ContentResult('test'));
}
else {
// set exception error message to result
$actionExecutedContext->setResult(new ContentResult($ex->getMessage()));
}
}
}
namespace RootNamespaceOfYourApp\Filters;
use PhpMvc\ActionFilter;
use PhpMvc\JsonResult;
class ExceptionToJson extends ActionFilter {
// error handler
public function exception($exceptionContext) {
// set JsonResult to action result
$exceptionContext->setResult(
new JsonResult(
array('message' => $exceptionContext->getException()->getMessage())
)
);
// exception is handled
$exceptionContext->setExceptionHandled(true);
}
}
namespace RootNamespaceOfYourApp\Controllers;
use PhpMvc\OutputCache;
use PhpMvc\OutputCacheLocation;
use PhpMvc\Controller;
class OutputCacheController extends Controller {
public function __construct() {
// caching for 10 seconds of the results of all actions of this controller
OutputCache::setDuration('.', 10);
// caching for 30 seconds of the results of the action of thirty
OutputCache::setDuration('thirty', 30);
}
public function index($time) {
return $this->content('time => ' . $time);
}
public function thirty($time) {
return $this->content('time => ' . $time);
}
}
$ composer
apache
<IfModule mod_rewrite.c>
RewriteEngine On
# redirect /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
# process all requests through index.php, except for actually existing files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [QSA,L]
</IfModule>