Download the PHP package webx/routes without Composer
On this page you can find all versions of the php package webx/routes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package routes
WebX-Routes - A PHP controller framework
Main features and design goals of webx-routes:
- Simplicity - Easy to follow request/response routes to business logic in a natural hierarchical model.
- Scalability - never load unnecessary files
- Dependency injection (IOC) everywhere.
- Testability - clear separation of view- and business objects by glueing it all together.
Getting started
In composer.json
add:
Writing your first Routes index.php public/index.php
Will generate JSON response:
Built-in ResponseTypes in Routes
Routes supports the following ResponseTypes out of the box
JsonResponse
Renders data as Json (Default ResponseType).TemplateResponseType
Renders data with a template (Twig)RawResponseType
Renders data as is.DownloadResponseType
Renders data as a downloadable file.RedirectResponseType
301 or 302 redirect to a different url.FileContentResponseType
Sends a file's content to browser with auto detecting content-type.
Routing in Routes
Routing in Routes - with url parameters example
Will render:
hello
for request on/hello
default
for request on/
Route switches:
Route switches are evaluated top-down. If a route-switch is executed no further switches, in same the scope, are evaluated and executed.
The following route switches are supported
onAlways($action)
Executes without evaluation.onTrue($expression,$action)
Executes if$expression
evaluates totrue
onSegment("url-segment",$action)
Evaluates the current url segment (complete url exploded by/
). Within a route-switch match the current url-segment will advance one position.onMatch("reg-exp",$action)
Evaluates the reg-exp against a string (url is default). Matched parameters in the reg-exp will be used if the same variable name is used in the$action
;
Using Twig
page.twig
Reading input
Routes provides a unified and type-safe way to read request input from query parameters and json- form-encoded requests.
Configuring Twig
Load a configuration changetwig
(can be any name) at Bootstrap time.
Example: To change Twigs tag-delimeters to {{{
and }}}
(To simplify mixed Angular and Twig in the same page).
Override the setting for TemplateResponseType
to add a configurator for Twig
config/changetwig.php
:
Loading configurations and the IOC container
All logic, in Routes, is executed in . An action can be either a:
- (In format "ControllerClass#method")
To support lazy loading of configurations Routes allows actions to be defined as an array
in the format:
[$action,"config1","config2","configN"]
src/MyBusiness/Impl/Services/AdminService.php
config/admin.php
:
Config file anatomy
The config files, that may be loaded at any level, are normal php files that are expected to return an array
.
Minimal config file (that does nothing):
Ioc container
The WebX-Ioc container is embedded in the WebX-Routes framework. WebX-Routes supports dynamic registration / static invokation of services in the config ioc
section.
Dynamically register a service with the WebX-Ioc container.
config/someconfig.php
Working with Controllers
Routes support a more traditional controller structure as well. Controllers are simple classes with their methods and constructors invoked with IOC support.
Routes supports $action
to be defined as a string
in the format ControllerClass#method
src/MyBusiness/Controllers/AdminController.php
Defining default namespaces for loading controllers
Full class names can be skipped by adding namespaces in the namespaces
section of a dynamic configuration.
config/admin.php
:
Creating your own ResponseType
To implement your own ResponseType simply create an interface that extends ResponseType
with an implementation configure it with ioc/register
in a config file. See bootstrap_config.php
of how to configure.
Configuring your own ResponseType
in your config file:
Configuring Routes
Standard configuration in Routes is based on the applications directory relativly to the $_SERVER['DOCUMENT_ROOT']
.
Configuring RoutesBootstrap
The default directory structure for a Routes application:
Tests
Execute in root directory