Download the PHP package serwisant/serwisant-silex without Composer

On this page you can find all versions of the php package serwisant/serwisant-silex. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package serwisant-silex

serwisant/serwisant-silex

A minimal replacement for Silex 2 (abandoned in 2018), built on current Symfony components (HttpKernel, Routing, HttpFoundation, EventDispatcher, ErrorHandler, Twig Bridge) and Pimple.

It preserves the Silex API to the extent used in Serwisant projects, so migration usually boils down to replacing use Silex; with use Serwisant\Silex;.

Requirements: PHP 8.1+, Symfony 6.4 / 7.4 / 8.x, Twig 3.21+, Pimple 3.6+.

Table of contents

Installation

To work on the package code locally, you can use a path repository (e.g. in ~/.composer/config.json):

Quick start

Application configuration

Serwisant\Silex\Application is a Pimple container, so parameters and services are set like array entries: in the constructor or later via $app['key'] = value.

Parameter Default Description
debug false Error page with a stack trace, strict_variables and dump() in Twig
charset UTF-8 Encoding of responses, Twig and escape()
env – Optional environment name, available in Twig as global.environment

Routing

The get(), post(), put(), patch(), delete(), options() methods register a route for the given HTTP method, match() – for any method. Each returns a Serwisant\Silex\Controller object with a fluent API:

Method Description
bind('name') Route name, used by path() / url()
assert('variable', 'regexp') Requirement for a route variable, e.g. '\d+'. When it does not match, 404 is returned
value('variable', $default) Default value – the variable becomes optional
convert('variable', $callback) Converter callback($value, Request), e.g. casting to int or loading an entity
method('GET\|POST') Allowed HTTP methods (for match())
host('{sub}.example.com') Required host
requireHttp() / requireHttps() Required scheme
before($cb) / after($cb) Route middleware, see Middleware

Controllers

A controller can be:

Controller arguments are injected:

The order of arguments is arbitrary.

A controller may return:

Grouping routes – mount()

Routes can be grouped under a common prefix. mount() accepts a ControllerCollection, a callable or an Api\ControllerProviderInterface object:

Settings called on a collection (assert(), value(), convert(), method(), host(), requireHttp(), requireHttps(), before(), after()) apply to all of its routes – including those added later. Collections can be nested via $collection->mount('/v2', $otherCollection).

Middleware

Application middleware

Application middleware only runs for the main request (not for sub-requests). The order can be changed with a priority: $app->before($cb, Silex\Application::EARLY_EVENT) or Silex\Application::LATE_EVENT.

Route middleware

Call order

  1. $app->before() – application middleware
  2. ->before() – route middleware
  3. controller
  4. view handlers (if the controller did not return a Response)
  5. ->after() – route middleware
  6. $app->after() – application middleware
  7. sending the response
  8. $app->finish()

Error handling

View handlers

They allow a controller to return data instead of a Response. The type of the first argument determines which results the handler processes:

A handler returning a Response ends processing. Any other returned value goes to the next handler (e.g. object → array → JSON), and a string is converted to a Response.

Responses – helper methods

Method Returns
$app->redirect($url, $status = 302) RedirectResponse
$app->json($data, $status = 200, $headers = []) JsonResponse
$app->stream($callback, $status = 200, $headers = []) StreamedResponse – the callback outputs content via echo
$app->sendFile($file, $status, $headers, $disposition) BinaryFileResponse; $disposition e.g. ResponseHeaderBag::DISPOSITION_ATTACHMENT
$app->render($template, $parameters) Response with the rendered Twig template
$app->renderView($template, $parameters) string with the rendered template
$app->path($route, $parameters) / $app->url(...) path / absolute URL of the route
$app->escape($text) text safe to insert into HTML
$app->abort($code, $message) throws HttpException

sendFile() detects the file's MIME type if the symfony/mime package is installed. Without it, application/octet-stream is sent, unless the Content-Type header is given explicitly.

Services and providers

Services are defined as in Pimple:

A provider is registered with $app->register(new Provider(), ['parameter' => 'value']). Parameters passed in the second argument override the defaults set by the provider.

Services always available

Key Description
dispatcher EventDispatcher, a listener can also be added via $app->on($event, $cb)
kernel HttpKernel
request_stack RequestStack – current request: $app['request_stack']->getCurrentRequest()
resolver, argument_resolver controller and argument resolvers
routes RouteCollection
url_generator, request_matcher, request_context routing
controllers, controllers_factory main route collection and factory of new collections

Built-in providers

RoutingServiceProvider

Registered automatically in the constructor. Calling register() again changes nothing – it can be left in place for Silex compatibility.

As in Silex, a GET/HEAD request for a path that differs from the defined one only by a trailing / (e.g. /blog with a /blog/ route) is redirected (301) to the route's path. The same applies to routes with requireHttps().

TwigServiceProvider

By default debug and strict_variables follow $app['debug'], and charset follows $app['charset'].

Template functions:

Global variables:

AssetServiceProvider

SessionServiceProvider

CorsServiceProvider

CORS headers, parameters compatible with devimteam/silex-cors-provider:

ServiceControllerServiceProvider

Allows defining controllers as methods of services from the container, which makes dependency injection easier:

Custom providers

A provider implements Pimple\ServiceProviderInterface. Optionally it may also implement:

Both methods are called once, when handling the first request: first subscribe() of all providers, then boot().

Testing

Application implements HttpKernelInterface, so a request can be handled without an HTTP server:

For POST routes: Request::create('/contact', 'POST', ['message' => ['body' => 'content']]).

Migrating from Silex 2

  1. In composer.json replace silex/silex with serwisant/serwisant-silex. Remove redundant packages: symfony/twig-bridge, symfony/asset etc. are dependencies of this package.
  2. Replace use Silex; / Silex\ with use Serwisant\Silex; / Serwisant\Silex\.
  3. Custom providers: change the types in signatures to Serwisant\Silex\Api\... and add void return types. Replace devimteam/silex-cors-provider with Serwisant\Silex\Provider\CorsServiceProvider.
  4. Check the changes in Symfony itself between 3.x/4.x and 6.4+ that are not caused by this package. E.g. $request->get() is deprecated since Symfony 7.4 – use $request->query->get(), $request->request->get() instead, and for arrays $request->request->all('key').

Differences from Silex

Not supported


All versions of serwisant-silex with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
pimple/pimple Version ^3.6
symfony/asset Version ^6.4 || ^7.4 || ^8.0
symfony/error-handler Version ^6.4 || ^7.4 || ^8.0
symfony/event-dispatcher Version ^6.4 || ^7.4 || ^8.0
symfony/http-foundation Version ^6.4 || ^7.4 || ^8.0
symfony/http-kernel Version ^6.4 || ^7.4 || ^8.0
symfony/routing Version ^6.4 || ^7.4 || ^8.0
symfony/twig-bridge Version ^6.4 || ^7.4 || ^8.0
twig/twig Version ^3.21
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package serwisant/serwisant-silex contains the following files

Loading the files please wait ...