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.
<?phprequire_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 applicationnamespaceRootNamespaceOfYourApp\Controllers;
// import the base class of the controllerusePhpMvc\Controller;
// expand your class with the base controller classclassHomeControllerextendsController{
publicfunctionindex(){
// 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();
}
}
usePhpMvc\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);
classAnyControllerextendsPhpMvc\Controller{
publicfunctionexample(){
$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 viewreturn $view;
}
}
classTestControllerextendsController{
publicfunction__construct(){
// add filter TestFilter to all actions of the controller
Filter::add('TestFilter');
// add filter ExceptionToJson to error action
Filter::add('error', 'ExceptionToJson');
}
publicfunctionindex(){
return$this->content('Hello, world!');
}
publicfunctionother(){
return$this->view('~/views/home/other.php');
}
publicfunctionanyname(){
return$this->view();
}
publicfunctionerror(){
thrownew \Exception('Any error here!');
}
}
namespaceRootNamespaceOfYourApp\Filters;
usePhpMvc\ActionFilter;
usePhpMvc\ContentResult;
classTestFilterextendsActionFilter{
// action executed handlerpublicfunctionactionExecuted($actionExecutedContext){
// check exception resultif (($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()));
}
}
}
namespaceRootNamespaceOfYourApp\Filters;
usePhpMvc\ActionFilter;
usePhpMvc\JsonResult;
classExceptionToJsonextendsActionFilter{
// error handlerpublicfunctionexception($exceptionContext){
// set JsonResult to action result
$exceptionContext->setResult(
new JsonResult(
array('message' => $exceptionContext->getException()->getMessage())
)
);
// exception is handled
$exceptionContext->setExceptionHandled(true);
}
}
namespaceRootNamespaceOfYourApp\Controllers;
usePhpMvc\OutputCache;
usePhpMvc\OutputCacheLocation;
usePhpMvc\Controller;
classOutputCacheControllerextendsController{
publicfunction__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);
}
publicfunctionindex($time){
return$this->content('time => ' . $time);
}
publicfunctionthirty($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>