Download the PHP package php-mvc-project/php-mvc without Composer
On this page you can find all versions of the php package php-mvc-project/php-mvc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download php-mvc-project/php-mvc
More information about php-mvc-project/php-mvc
Files in php-mvc-project/php-mvc
Package php-mvc
Short Description Implementation of the MVC (Model-View-Controller) architectural pattern in PHP.
License MIT
Informations about the package php-mvc
PHP MVC
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
Server Configuration
The server must send the entire request to the ./index.php
file.
Apache
nginx
Basic Usage
Create the following structure in the project root directory:
./index.php
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
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:
And adhere to the following rules:
-
Folder names must be in lowercase.
-
The views filenames must be in lowercase.
- 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.
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:
Use the helper class PhpMvc\View
to customize the behavior of the view:
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 (_).
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:
Below is an example of obtaining a model sent by the POST method:
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:
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
./filters/TestFilter.php
./filters/ExceptionToJson.php
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.
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:
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.
You can access the cache via an instance of HttpContextBase
.
For example, in the controller:
In the view:
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.
License
The MIT License (MIT)
Copyright © 2018, @meet-aleksey