PHP Classes
elePHPant
Icontem

PHP MVC Project: Framework that implements the MVC design pattern

Recommend this page to a friend!
  Info   View files Documentation   Demos   Videos Videos   View files View files (112)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-06-02 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 183 This week: 2All time: 8,271 This week: 250Up
Version License PHP version Categories
php-mvc-project 1.2MIT/X Consortium ...7Libraries, PHP 7
Description Author

This package is a framework that implements the MVC design pattern.

It provides several classes that provide common functions that are necessary in most Web applications. Currently it provides classes for:

- Templates
- Routing
- Filters
- Cache
- Validation
- Data annotation
- Security

  Performance   Level  
Name: Aleksey <contact>
Classes: 2 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 1x

Details

PHP MVC

PHP from Packagist License GitHub release Packagist

The best implementation of the Model-View-Controller architectural pattern in PHP!

Features

  • Templates
  • Routing
  • Filters
  • Cache
  • Validation
  • Data annotation
  • Security

Requirements

  • PHP 7.x

Installation

$ composer require php-mvc-project/php-mvc

Server Configuration

The server must send the entire request to the ./index.php file.

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>

nginx

location / {
  try_files $uri $uri/ /index.php?$args;
}

Basic Usage

Create the following structure in the project root directory:

.
??? controllers           # controllers folder
???? HomeController.php   # class of the home controller
???? *Controller.php      # classes of others controllers
??? models                # models folder
??? views                 # views folder
???? home                 # views folder of the home controller
???? ...                  # folders for other controllers
???? shared               # shared views
??? index.php             # index file

./index.php

<?php
// use aoutoload (recommended)
require_once getcwd() . '/vendor/autoload.php';
// or include the required files
// require_once getcwd() . '/vendor/php-mvc-project/php-mvc/src/index.php';

// import the AppBuilder class
use 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();

> IMPORTANT: You must use namespaces in your application. > Be sure to specify the root namespace of your application using the AppBuilder::useNamespace(string) function.

./controllers/HomeController.php

<?php
// 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();
    }

}

> IMPORTANT: The names of all controller classes must end with the Controller suffix. > For example: HomeController, AccountController, TestController etc.

Structure

Your projects must implements the strict structure:

.
??? content               # static content (images, css, scripts, etc)
???? ...                  # any files and folders
??? controllers           # controllers folder
???? HomeController.php   # class of the home controller
???? *Controller.php      # classes of others controllers
??? filters               # filters folder
???? *.php                # classes of models
??? models                # models folder
???? *.php                # classes of models
??? views                 # views folder
???? ...                  # views for specific controllers
???? shared               # shared views
???? ...                  # common view files that are not associated with specific controllers
??? index.php             # index file
??? ...                   # any of your files and folders

And adhere to the following rules:

  1. Folder names must be in lowercase.
  2. The views filenames must be in lowercase.
  3. The file names of the controllers must be specified in the camel style, with a capital letter. The names must end with the Controller suffix. For example: HomeController.php.

Models

The model is just classes. You can create any classes, with any structure.

It is recommended to adhere to the rule: the simpler, the better.

Using the static class Model, you can add metadata to a model in the constructor of the controller.

<?php
namespace RootNamespaceOfYourApp\Controllers;

use PhpMvc\Controller;
use PhpMvc\Model;

class AccountController extends Controller {

    public function __construct() {
        Model::set('join', 'join');

        Model::required('join', 'username');
        Model::required('join', 'email');
        Model::required('join', 'password');
        Model::required('join', 'confirmPassword');

        Model::display('join', 'username', 'Username');
        Model::display('join', '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);
    }

}

Views

The views files contain markup. Markup can be complete or partial.

Using the PhpMvc\Html class, you can create markup for HTML elements or output some views within other views.

For example:

<?php 
use PhpMvc\Html;
?>
<html>
<head>
  <title><?=Html::getTitle('Hello, world!')?></title>
</head>
<body>
  <h1>Hello, world!</h1>
  <?=Html::actionLink('Go to second view', 'second'))?><br />
  <?=Html::textBox('anyname')?><br />
  <?=Html::checkbox('rememberMe')?><br /><br />

  <!--Render ./view/shared/footer.php-->
  <?php Html::render('footer'); ?>
</body>
</html>

Use the helper class PhpMvc\View to customize the behavior of the view:

<?php 
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);
?>

<h1><?=isset($model) ? $model->anyProperty : 'empty'?></h1>

<!--...-->

Controllers

The controller classes names must match the controllers filenames. For example: file name is TestController.php, class name is TestController.

Each controller class must inherit from the PhpMvc\Controller class.

The controller classes must contain action methods.

The action names must match filenames of views. For example: view file is index.php, action name is index.

All methods of actions must have the modifier public.

The names of the action methods must not start with the underscore (_).

class HomeController extends PhpMvc\Controller {

  public function index() {
    return $this->view();
  }

  public function hello() {
    return $this->view();
  }

  public function other() {
    return $this->view();
  }

}

Each action can take any number of parameters.

The parameters can be received from the query string, or from the POST data.

The following example shows the output of parameters retrieved from the query string:

class TestController extends PhpMvc\Controller {

  public function get($search = '', $page = 1, $limit = 10) {
    return $this->content(
      'search: ' . $search . chr(10) .
      'page: ' . $page . chr(10) .
      'limit: ' . $limit
    );
  }

}

Request:
GET /test/get?search=hello&page=123&limit=100

Response:
search: hello
page: 123
limit: 100

Below is an example of obtaining a model sent by the POST method:

class AnyNameModelClass {

  public $search;

  public $page;

  public $limit;

}

class TestController extends PhpMvc\Controller {

  public function post($anyName) {
    return $this->content(
      'search: ' . $anyName->search . chr(10) .
      'page: ' . $anyName->page . chr(10) .
      'limit: ' . $anyName->limit
    );
  }

}

Request:
POST /test/post

search=hello&page=123&limit=100

Response:
search: hello
page: 123
limit: 100

Methods of action can return different results, in addition to views.

You can use the ready-made methods of the base class of the controller to output the data in the required format:

  • $this->view([string $viewOrModel = null[, object $model = null[, string $layout = null]]])
  • $this->json(mixed $data[, int $options = 0[, int $depth = 512]])
  • $this->file(string $path[, string $contentType = null[, string|bool $downloadName = null]])
  • $this->content(string $content[, string $contentType = 'text/plain'])
  • $this->statusCode(int $statusCode[, string $statusDescription = null])
  • $this->notFound([string $statusDescription = null])
  • $this->unauthorized([string $statusDescription = null])
  • $this->redirect(string $url)
  • $this->redirectPermanent(string $url)
  • $this->redirectPreserveMethod(string $url)
  • $this->redirectPermanentPreserveMethod(string $url)
  • $this->redirectToAction(string $actionName[, string $controllerName = null[, array $routeValues = null[, string $fragment = null]]])

Instead of the presented methods, you can independently create instances of the desired results and return them:

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;
  }

}

All result classes implement the ActionResult interface. You can create your own result classes!

Filters

Filters allow you to add handlers before and after the action. And also handle errors of the action execution.

The filters must be in the ./Filters folder.

Each filter must be inherited from the PhpMvc\ActionFilter class.

Filters can be global, or work at the level of an individual controller, or action.

Filters for specific controller or action can be set in the controller's constructor:

./controllers/TestController.php

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!');
  }

}

./filters/TestFilter.php

<?php
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()));
    }
  }

}

./filters/ExceptionToJson.php

<?php
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);
  }

}

Routing

You can set routing rules using the AppBuilder::routes() function, which expects the function as a parameter. When called, an instance of RouteProvider will be passed to the function.

The add(string $name, string $template[, array $defaults = null[, array $constraints = null]]) method allows you to add a routing rule.

The ignore(string $template[, $constraints = null]) method allows you to add an ignore rule.

AppBuilder::routes(function(RouteProvider $routes) {
  $routes->add('default', '{controller=Home}/{action=index}/{id?}');
});

The names of the routing rules must be unique.

The higher the rule in the list (the earlier the rule was added), the higher the priority in the search for a match.

In templates you can use any valid characters in the URL.

Use curly braces to denote the elements of the route.

Each element must contain a name.

A name can point to a controller, action, or any parameter expected by the action method.

For example, template is: {controller}/{action}/{yyyy}-{mm}-{dd}.

Action is:

public function index($yyyy, $mm, $dd) {
  return $this->content('Date: ' . $yyyy . '-' . $mm . '-' . $dd);
}

Request:
GET /home/index/2018-05-26

Response:
Date: 2018-05-26

After the element name, you can specify a default value in the template. The default value is specified using the equals sign.

For example: {controller=home}/{action=index} - default controller is HomeController, default action is index(). The default values will be used if the address does not have values for the specified path elements. Simply put, for requests /home/index, /home and / will produce the same result with this template.

If the path element is optional, then the question (?) symbol is followed by the name. For example: {controller=home}/{action=index}/{id?} - id is optional, {controller=home}/{action=index}/{id} - id is required.

Route providers must implement the RouteProvider interface. The default is DefaultRouteProvider. If necessary, you can create your own route provider. Use the AppBuilder::useRouter(RouteProvider $routeProvider) method to change the route provider.

Caching

To use caching, you must call the AppBuilder::useCache(CacheProvider $cacheProvider) method, which should be passed to the cache provider instance.

The cache provider must implement the CacheProvider interface.

You can use the ready-made FileCacheProvider, which performs caching in the file system.

AppBuilder::useCache(new FileCacheProvider());

You can access the cache via an instance of HttpContextBase.

For example, in the controller:

$cache = $this->getHttpContext()->getCache();
$cache->add('test', 'hello, world!');
var_dump($cache->get('test'));

In the view:

$cache = View::getHttpContext()->getCache();
$cache->add('test', 'hello, world!');
var_dump($cache->get('test'));

For output caching, you can use the static OutputCache class.

Caching rules can be specified for both the controller and for an each action.

Cache rules should be specified in the constructor of the controller.

<?php
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);
    }
 
}

License

The MIT License (MIT)

Copyright © 2018, @meet-aleksey

  Default Template DemoExternal page  

Open in a separate window

  Files folder image Files  
File Role Description
Files folder image.vscode (1 file)
Files folder imagesrc (66 files)
Files folder imagetests (15 files, 2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. ReadMe file

  Files folder image Files  /  .vscode  
File Role Description
  Accessible without login Plain text file tasks.json Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file ActionContext.php Class Class source
  Plain text file ActionExecutedContext.php Class Class source
  Plain text file ActionExecutingContext.php Class Class source
  Plain text file ActionFilter.php Class Class source
  Plain text file ActionNameValidationException.php Class Class source
  Plain text file ActionResult.php Class Class source
  Plain text file AppBuilder.php Class Class source
  Plain text file AppContext.php Class Class source
  Plain text file Cache.php Class Class source
  Plain text file CacheProvider.php Class Class source
  Plain text file ContentResult.php Class Class source
  Plain text file Controller.php Class Class source
  Plain text file CrossSiteScriptingValidation.php Class Class source
  Plain text file DefaultRouteProvider.php Class Class source
  Plain text file EmptyResult.php Class Class source
  Plain text file ErrorHandlerEventArgs.php Class Class source
  Plain text file EventArgs.php Class Class source
  Plain text file ExceptionContext.php Class Class source
  Plain text file ExceptionResult.php Class Class source
  Plain text file FileCacheProvider.php Class Class source
  Plain text file FileCacheProviderConfig.php Class Class source
  Plain text file FileResult.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file Html.php Class Class source
  Plain text file HttpAntiForgeryException.php Class Class source
  Plain text file HttpContext.php Class Class source
  Plain text file HttpContextBase.php Class Class source
  Plain text file HttpContextInfo.php Class Class source
  Plain text file HttpRequest.php Class Class source
  Plain text file HttpRequestBase.php Class Class source
  Plain text file HttpRequestValidationException.php Class Class source
  Plain text file HttpResponse.php Class Class source
  Plain text file HttpResponseBase.php Class Class source
  Plain text file HttpSession.php Class Class source
  Plain text file HttpSessionProvider.php Class Class source
  Plain text file HttpStatusCodeResult.php Class Class source
  Plain text file IdleCacheProvider.php Class Class source
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Plain text file Info.php Class Class source
  Plain text file InternalHelper.php Class Class source
  Plain text file JsonResult.php Class Class source
  Accessible without login Plain text file Loader.php Aux. Auxiliary script
  Plain text file Model.php Class Class source
  Plain text file ModelDataAnnotation.php Class Class source
  Plain text file ModelState.php Class Class source
  Plain text file ModelStateEntry.php Class Class source
  Plain text file OutputCache.php Class Class source
  Plain text file OutputCacheLocation.php Class Class source
  Plain text file PathUtility.php Class Class source
  Plain text file RedirectResult.php Class Class source
  Plain text file RedirectToActionResult.php Class Class source
  Plain text file Route.php Class Class source
  Plain text file RouteCollection.php Class Class source
  Plain text file RouteOptions.php Class Class source
  Plain text file RouteParsingException.php Class Class source
  Plain text file RouteProvider.php Class Class source
  Plain text file RouteSegment.php Class Class source
  Plain text file SelectListGroup.php Class Class source
  Plain text file SelectListItem.php Class Class source
  Plain text file UrlHelper.php Class Class source
  Plain text file UrlParameter.php Class Class source
  Plain text file ValidateAntiForgeryToken.php Class Class source
  Plain text file View.php Class Class source
  Plain text file ViewContext.php Class Class source
  Plain text file ViewNotFoundException.php Class Class source
  Plain text file ViewResult.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagemodels (3 files)
Files folder imagemvc (5 directories)
  Plain text file AppBuilderTest.php Class Class source
  Plain text file ArrayToObjectTest.php Class Class source
  Accessible without login Plain text file autoload.php Example Example script
  Plain text file DefaultRouteProviderTest.php Class Class source
  Plain text file FileCacheProviderTest.php Class Class source
  Plain text file FilterTest.php Class Class source
  Plain text file HtmlTest.php Class Class source
  Plain text file HttpContext.php Class Class source
  Plain text file HttpContextInfo.php Class Class source
  Plain text file HttpRequest.php Class Class source
  Plain text file HttpResponse.php Class Class source
  Plain text file OutputCacheTest.php Class Class source
  Plain text file PathUtilityTest.php Class Class source
  Plain text file RouteCollectionTest.php Class Class source
  Plain text file UrlHelperTest.php Class Class source

  Files folder image Files  /  tests  /  models  
File Role Description
  Plain text file ModelA.php Class Class source
  Plain text file ModelB.php Class Class source
  Plain text file ModelC.php Class Class source

  Files folder image Files  /  tests  /  mvc  
File Role Description
Files folder imagecontent (1 directory)
Files folder imagecontrollers (4 files)
Files folder imagefilters (5 files)
Files folder imagemodels (2 files)
Files folder imageviews (3 directories)

  Files folder image Files  /  tests  /  mvc  /  content  
File Role Description
Files folder imageimages (2 files)

  Files folder image Files  /  tests  /  mvc  /  content  /  images  
File Role Description
  Accessible without login Image file mvc.png Data Auxiliary data
  Accessible without login Image file php.png Data Auxiliary data

  Files folder image Files  /  tests  /  mvc  /  controllers  
File Role Description
  Plain text file AccountController.php Class Class source
  Plain text file FilterController.php Class Class source
  Plain text file HomeController.php Class Class source
  Plain text file OutputCacheController.php Class Class source

  Files folder image Files  /  tests  /  mvc  /  filters  
File Role Description
  Plain text file EmptyFilter.php Class Class source
  Plain text file ErrorFilter.php Class Class source
  Plain text file ExecutedWorldFilter.php Class Class source
  Plain text file ExecutingWorldFilter.php Class Class source
  Plain text file NoResultErrorFilter.php Class Class source

  Files folder image Files  /  tests  /  mvc  /  models  
File Role Description
  Plain text file HomePage.php Class Class source
  Plain text file Login.php Class Class source

  Files folder image Files  /  tests  /  mvc  /  views  
File Role Description
Files folder imageaccount (1 file)
Files folder imagehome (3 files)
Files folder imageshared (7 files)

  Files folder image Files  /  tests  /  mvc  /  views  /  account  
File Role Description
  Accessible without login Plain text file login.php Example Example script

  Files folder image Files  /  tests  /  mvc  /  views  /  home  
File Role Description
  Accessible without login Plain text file hello.php Aux. Auxiliary script
  Accessible without login Plain text file index.php Example Example script
  Accessible without login Plain text file prog.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  mvc  /  views  /  shared  
File Role Description
  Accessible without login Plain text file error.php Aux. Auxiliary script
  Accessible without login Plain text file footer.php Aux. Auxiliary script
  Accessible without login Plain text file header.php Aux. Auxiliary script
  Accessible without login Plain text file _default.php Aux. Auxiliary script
  Accessible without login Plain text file _empty.php Aux. Auxiliary script
  Accessible without login Plain text file _lite.php Aux. Auxiliary script
  Accessible without login Plain text file _parent.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:183
This week:2
All time:8,271
This week:250Up