Download the PHP package iassasin/easyroute without Composer
On this page you can find all versions of the php package iassasin/easyroute. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iassasin/easyroute
More information about iassasin/easyroute
Files in iassasin/easyroute
Package easyroute
Short Description Very lightweight and simple router for PHP
License MIT
Homepage https://github.com/iassasin/easyroute
Informations about the package easyroute
Easyroute
Simple router do not require a lot of setups, install in few minutes and just works.
Installation
To start using router complete 4 simple steps:
-
Install
easyroute
via composer: -
Setup yours routes in root directory of project. For example in file
routes.php
: -
Tell web server redirect all requests (except static
assets/
) to router via.htaccess
: - Create first controller in
controllers/home.php
:
That's it!
Note that file name must match :controller
name from URL template and controller class name must match :controller
name with prefix Controller
. In example above route /home/index
matches file controllers/home.php
and class ControllerHome
.
Useful features
Route with parameters
Route is a regex string with simpler syntax for naming route arguments. For example:
/:controller/:action/:arg
(all parameters are required)/(foo|bar)-:arg/gotcha
(regex and partial parameter matching. Valid are/foo-123/gotcha
,/bar-qwerty/gotcha
, but not/qwerty/gotcha
)/:arg?
(parameterarg
can be ommited, but not/
. Valid are/
,/param
, but not/sub/param
)/(:controller:(/:action:(/:arg)?)?)?
(all parameters not required. Valid are/
,/home
,/home/index/val
, but not/home/
,/home/index/
)
Parameter name must match regex [a-zA-Z_0-9]+
.
By default :parameter
match regex [^\/]+
, but you can use your own filter:
If you need to use (
to match group, but not to filter parameter, use trailing :
in parameter name: /:controller:(postfix1|postfix2)
will match /homepostfix1
with controller = 'home'
.
Built-in simple dependency injection container
You can use built-in dependency injection container to split app code into services (which will be loaded via autoload) and simply use it in controller's actions:
Services constructors can use dependency injection too and require another services via same syntax.
Class SampleContainer
impletents Psr\Container\ContainerInterface
.
Also you can use another dependency injection container:
Request class
Using dependency injection container you have access to Request
service that provides access to request parameters.
Request
has fields listed below:
query
-$_GET
arrayrequest
-$_POST
arrayattributes
- assoc array with any data, which you can set atRequest
instance creationcookies
-$_COOKIE
arrayfiles
-$_FILES
arrayserver
-$_SERVER
array
Each field is instance of Parameters
class and has methods:
get(string $name)
- get parameter by it's namehas(string $name)
- does parameter with$name
existsall()
- get all parameters in array
Request
provide some useful methods:
getContent()
- get whole http post content as stringgetClientIP()
- returns$this->server->get('REMOTE_ADDR')
getScriptName()
- returns$this->server->get('SCRIPT_NAME')
getScheme()
- returns$this->server->get('REQUEST_SCHEME')
getHost()
- returns$this->server->get('SERVER_NAME')
getUri()
- returns$this->server->get('REQUEST_URI')
getMethod()
- returns$this->server->get('REQUEST_METHOD')
getProtocol()
- returns$this->server->get('SERVER_PROTOCOL')
Response class
Response
class used to return data to client. Return Response
instances from controller's actions instead of manual data send using echo or etc.
Built-in response classes
There is 3 built-in response classes:
Response
- base class for all responses, has methods:__construct(string $content, int $statusCode = 200, array $headers = [])
getStatusCode()
,setStatusCode(int $code)
- get/set http status codegetContent()
,setContent(string $content)
- get/set http response contentgetHeaders()
,setHeaders(array $headers)
- get/set http headerssetHeader(string $name, string $value)
- set single headersend()
- send response to client, called from handlers
Response404
- extendsRequest
for default http 404 error with:__construct(string $url, array $headers = [])
getUrl()
- get URL caused 404 error
Response500
- extendsRequest
for default http 500 error. Generated if any exception thrown during route processing.__construct(\Throwable $exception = null, array $headers = [])
getException()
- get exception thrown during route processing
ResponseJson
- extendsRequest
for sending json responses, also set correct httpContent-Type
header.__construct(object|array $data, int $statusCode = 200, array $headers = [])
getData()
,setData(object|array $data)
- set source data to send to client- Note that
setContent
can't be used and throwsLogicException
Custom handlers for response classes
Set custom handlers for Response
classes:
And custom handlers for any http status code:
Note 1: you can use both handlers set, but response handlers will be called first (and can stop calling status handlers).
Note 2: you can set handler for parent response class to match all childs, child handlers will be always called first: from most child to first parent. So, you can match all responses setting handler for
Response::class
.
Subdirectories for controllers
Separate zones with subdirectories:
Filtering access
Use RouteFilter
to prevent access to some routes:
Custom controller class name prefix
Set custom controller class name prefix (default - Controller
):