Tested on php v5.4+
No compilation, optimized
Separate the routes or chain them
Create custom callable plugins, manipulate with return value etc.
Pass the error response to child routes
Manage views, *.properties files, languages etc.
use php_mvc\Route;
Route::builder()
->setRequestOrigin(/*Origin*/)
->setRequestHost(/*STRING OR ARRAY*/)
->setRequestReferer(/*STRING OR ARRAY*/)
->setRequestPath(/*STRING OR ARRAY*/)
->setRequestMethod(/*STRING OR ARRAY*/)
->setRequestContentType(/*STRING OR ARRAY*/)
->setResponseError(/*INTEGER OR ARRAY*/)
->setResponseBody(function (/*CALLABLE & PLUGINS*/) {
return ...
})
->setResponseBodyError(function (/*CALLABLE & PLUGINS*/) {
return ...
})
->setResponseBodyErrorParent(function (/*CALLABLE & PLUGINS*/) {
return ...
})
->addRoute(Route::builder()->...);
Route::builder()->...
Apache
Options +FollowSymLinks -Indexes
RewriteEngine On
#By default public files are placed inside "/inc/" folder
RewriteCond %{REQUEST_URI} !^/inc/
#MVC core file
RewriteRule ^ \.core/index\.php [L]
If the first route setRequestPath in route group is not provided, it starts with '/'
use php_mvc\Route;
use php_mvc\RequestMethod;
Route::builder()
->setRequestPath('/login') /*GET /login */
->addRoute(Route::builder()
->setRequestMethod(RequestMethod::POST)
->setRequestPath('/request')) /*POST /login/request */
The pattern may contain special characters
1. '?' one character
2. '*' zero or more characters
3. '{' and '}' url pattern
Example
1. '/user?/'
2. '/profile/user-{userid:[0-9]+}'
3. ['/login/*', '/register/*']
Default values but you can also use your own string
use php_mvc\Route;
Route::builder()
->setRequestMethod(...)
use php_mvc\RequestMethod;
RequestMethod::GET //default
RequestMethod::POST
RequestMethod::DELETE
RequestMethod::PUT
...
Example
1. 'POST'
2. RequestMethod::GET
3. [RequestMethod::GET, 'POST']
Default values but you can also use your own string
use php_mvc\Route;
Route::builder()
->setRequestContentType(...)
use php_mvc\ContentType;
ContentType::APPLICATION_JSON
ContentType::MULTIPART_FORM_DATA
ContentType::TEXT_HTML
ContentType::TEXT_PLAIN
...
Example
1. 'text/plain'
2. ContentType::TEXT_HTML
3. [ContentType::TEXT_HTML, 'text/plain']
Default values but you can also use your own integer
If provided error code is not integer it will not trigger response error
use php_mvc\Route;
Route::builder()
->setResponseError(...) //Force error response
use php_mvc\RequestError;
RequestError::METHOD
RequestError::CONTENT_TYPE
RequestError::REFERER
...
Usage
Route::hasError() //array
Route::hasError(RequestError::METHOD) //boolean
The error will be triggered if setRequest params will not match
use php_mvc\Route;
use php_mvc\RequestMethod;
Route::builder() //Error return will be 'error 1';
->setRequestMethod(RequestMethod::GET)
->setResponseBodyError(function() {
return 'error 1';
})
->addRoute(Route::builder() //Error return will be 'error 2';
->setRequestPath('login')
->setRequestMethod(RequestMethod::POST)
->setResponseBodyError(function() {
return 'error 2';
})
);
The error will be triggered if setRequest params will not match
use php_mvc\Route;
use php_mvc\RequestMethod;
Route::builder() //Error return will be 'error parent';
->setRequestMethod(RequestMethod::GET)
->setResponseBodyErrorParent(function() {
return 'error parent';
})
->addRoute(Route::builder() //Error return will be 'error parent';
->setRequestPath('login')
->setRequestMethod(RequestMethod::POST)
);
Response will catch array and produce JSON, XML with appropriate header
use php_mvc\Response;
setResponseBody(function(Response $response) {
return ['login' => true];
//{"login" : true}
$response->setResponseType(Response::XML);
return ['login' => 'true'];
//<login>true</login>
return ['name' => ['Mark','Alex']];
/*<name>
<item>Mark</item>
<item>Alex</item>
</name>*/
});
$response->setHeader(/*name*/,/*value*/)
$response->setHeaderArray(/*array or null*/)
$response->setLocation(/*url*/,/*response code*/)
$response->setResponseType(/*integer*/)
Response::JSON
Response::XML
Response::STRING
HttpRequest
use php_mvc\Route;
use php_mvc\HttpRequest;
Route::builder()
->setRequestPath('/user-{userid:[0-9]+}')
->setResponseBody(function (HttpRequest $request) {
return $request; //Show all data from request
});
$request->getPathVariable('userid')
$request->getPathVariables()
$request->getPostVariable(/*string*/)
$request->getPostVariables()
$request->getQueryVariable(/*string*/)
$request->getQueryVariables()
$request->getRequestHeader(/*string*/)
$request->getRequestHeaders()
PathVariable
use php_mvc\Route;
use php_mvc\HttpRequest;
Route::builder()
->setRequestPath('/user-{userid:[0-9]+}')
->setResponseBody(function (PathVariable $userid) {
return $userid; //Show value of userid
});
QueryVariable
use php_mvc\Route;
use php_mvc\QueryVariable;
Route::builder()
->setResponseBody(function (QueryVariable $token) {
if ($token->isPresented())
return $token; //Show value of token
return 'QueryVariable not presented';
});
PostVariable
use php_mvc\Route;
use php_mvc\PostVariable;
Route::builder()
->setResponseBody(function (PostVariable $username) {
if ($username->isPresented())
return $username; //Show value of username
return 'PostVariable not presented';
});
RequestFile
use php_mvc\Route;
use php_mvc\RequestFile;
Route::builder()
->setResponseBody(function (RequestFile $name) {
if ($name->isPresented())
return $name->getResult(); //Show array
return 'RequestFile not presented';
});
RequestHeader
use php_mvc\Route;
use php_mvc\RequestHeader;
Route::builder()
->setResponseBody(function (RequestHeader $content_type) {
if ($content_type->isPresented())
return $content_type; //Show value of content_type
return 'RequestHeader not presented';
});
use php_mvc\Route;
use php_mvc\HttpSession;
Route::builder()
->setResponseBody(function (HttpSession $sessionName) {
if ($sessionName->isPresented())
return $sessionName; //Show value of sessionName
return 'HttpSession not presented';
});
$sessionName->isPresented()
$sessionName->setValue(/*string*/)
$sessionName->unsetSession()
use php_mvc\Route;
use php_mvc\HttpCookie;
Route::builder()
->setResponseBody(function (HttpCookie $login_token) {
if ($login_token->isPresented())
return $login_token; //Show value of login_token
return 'HttpCookie not presented';
});
$login_token->isPresented()
$login_token->unsetCookie()
View path - project/view/test.html
Creates view folder if not created
use php_mvc\Route;
use php_mvc\ModelAndView;
Route::builder()
->setResponseBody(function (ModelAndView $model) {
return 'home'; //View located => /project/view/home.html
});
$model->setViewName(/*viewName*/)
$model->addParameter(/*name*/, /*value*/)
$model->addParameterArray(/*array*/)
//modify all chained views
$model->addParameterParent(/*name*/, /*value*/)
$model->addParameterParentArray(/*array*/)
Chain views
$model->addParameter('header', new ModelAndView('header'))
$model->addParameter('login', ModelAndView::builder()->setViewName('login'))
Possibilities - comments, replace inside, long text etc.
Properties path - project/properties/test.properties
#Database
username = root
password = g653Hj5
#Car
car = Nissan ${model}
model = Juke
#Long text
story = The ${car} drove 25,000 miles.\
I heard a sound.\nI hope it wasn't a car.
Creates properties folder if not created
use php_mvc\Route;
use php_mvc\SharedProperties;
Route::builder()
->setResponseBody(function (SharedProperties $properties) {
//$properties->initFile('config', ['username' => 'root', 'password' => 'g653Hj5']);
$properties->getSharedProperties('config');
return $properties; //show replaceable
});
$properties->getReplaceable()
$properties->getSharedProperties(/*path with name*/)
$properties->initFile(/*path with name*/, /*array*/, /*boolean*/)
$properties->getString(/*string*/)
$properties->getBoolean(/*string*/)
$properties->getInt(/*string*/)
$properties->getFloat(/*string*/)
$properties->getArray(/*string*/)
Locale path - project/properties/locale/
Creates locale folder if not created
Locale folder should include specific lang folders like "locale/lv/" or "locale/rus/" and than set cookie value to folder name lv or rus
Default language files need to be inside locale folder, for example locale/eng.properties
Locale file names does not matter, for example you can write (main, cars, random).properties
use php_mvc\Route;
use php_mvc\Locale;
Route::builder()
->setResponseBody(function (Locale $locale) {
return $locale;
});
$locale->filterFiles(/*string or array*/)
$locale->getStructure();
$locale->getLocale(/*string or null*/)
$locale->getCookieName()
$locale->getCookieValue()
$locale->getReplaceable()
$locale->setCookieName(/*string*/)
$locale->setCookie(/*path name*/, /*time = null*/, /*path = null*/, /*domain = null*/)
$locale->unsetCookie()
$locale->getString(/*string*/)
$locale->getInt(/*string*/)
$locale->getFloat(/*string*/)
$locale->getBoolean(/*string*/)
$locale->getArray(/*string*/)
Route will check and set appropriate "Access-Control-" headers
use php_mvc\Route;
Route::builder()
->setRequestOrigin(...)
use php_mvc\Origin;
$origin = Origin::builder()
->setServer(/*string or array*/)
->setMethod(/*string or array*/)
->setHeader(/*string or array*/)
->setCredentials()
->setMaxAge(/*int*/);
$origin = new Origin(/*server*/, /*method = null*/, /*header = null*/, /*credentials = null*/, /*maxAge = null*/);
$origin->__getServer()
$origin->__getResult()
use php_mvc\MysqlDB;
setResponseBody(function (MysqlDB $config) {...});
$config = new MysqlDB('config'); /*project/properties/config.properties*/
$execute = $config->mysql_query(/*string*/, /*array*/);
...
//Query bind params are optional, type => s,i,d,b
$query = 'SELECT * FROM users WHERE '.
'name = ? OR './/------ ? => Mark
'name = %s OR './/---- %s => Mason (string)
'name = ? OR './/------ ? => Jacob
'name = %1 OR './/---- %1 => Mark
'name = %2s OR './/-- %2s => Mason (string)
'name = :name';//-- :name => Brian
$params = [
'Mark',
'Mason',
'name' => 'Brian',
'Jacob'
];
$config->mysql_query($query, $params);
...
$query = 'SELECT * FROM users WHERE '.
'id = ? OR './/----- ? => 10 (integer)
'id IN (?) OR './/-- ? => 11,12 (each integer)
'id IN (%i)';//---- %i => 13,14 (each integer)
$params = [
[MysqlDB::INTEGER, '10'],
[MysqlDB::INTEGER, ['11','12']],
[['13','14']]
];
$config->mysql_query($query, $params);
...
MysqlDB::STRING //s
MysqlDB::INTEGER //i
MysqlDB::DOUBLE //d
MysqlDB::BLOB //b
onStringReturn is simplified version of toString that shows only a string, no matter what you return
onResponseBodyReturn can modify response body output by returning value
use php_mvc\Route;
use php_mvc\CustomPlugin;
Route::builder()
->setRequestPath('/profile/user-{userid:[0-9]+}')
->setResponseBody(function (CustomPlugin $pluginText) {
return 'return value to catch or modify';
});
namespace php_mvc;
class CustomPlugin extends PluginHelper
{
protected $paramName;
/* You can also use string $this->paramName.
Plugin params are trimmed (_) so '_pluginText' will produce 'pluginText' and so on to avoid same naming conflicts.
In this case it would be 'pluginText' because 'CustomPlugin $pluginText'. */
protected $pathVariables;
/* You can also use array $this->pathVariables.
In this case it would be array with value 'userid' => 'string that matches regex [0-9]+'. */
protected function __onStringReturn(){ //Optional
return ... //produces string of any value on __toString()
}
protected function __onResponseBodyReturn($original, $modified){ //Optional
//$original => unchanged return value.
/*Return will modify future return value under $modified.
Returning "null" will not change $modified.*/
return ... /*NULL OR STRING*/
}
}
api-key
developer api key
issuer
valid developer http(s) return url on client login success or cancel
format
xml, json is default
...
GET https://baltic.id/api-key?format=format HTTP/1.1
[ 'request' => true|false, 'token' => string ]
...
CLIENT https://baltic.id/token?return=issuer HTTP/1.1
Location: issuer?token=token
...
GET https://baltic.id/api-key?token=token&format=format HTTP/1.1
[ 'request' => true|false 'user' => [ 'id' => integer, 'created' => integer, 'secured' => true|false, 'verified' => true|false, 'general' => [ 'image' => [ 'base64' => string|null, 'modified' => integer|null ], 'name' => string|null, 'surname' => string|null, 'username' => string|null, 'country' => string(LV,LT,EE)|null ], 'birth' => [ 'day' => integer|null, 'month' => integer|null, 'year' => integer|null ], 'email' => array|list(item), 'number' => array|list(item) ] ]