Skip to content

Class Phalcon\Mvc\Micro

extends abstract class Phalcon\Di\Injectable

implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, ArrayAccess

Source on GitHub

With Phalcon you can create "Micro-Framework like" applications. By doing this, you only need to write a minimal amount of code to create a PHP application. Micro applications are suitable to small applications, APIs and prototypes in a practical way.

<?php

$app = new \Phalcon\Mvc\Micro();

$app->get(
    "/say/welcome/{name}",
    function ($name) {
        echo "<h1>Welcome $name!</h1>";
    }
);

$app->handle();

Methods

public __construct ([Phalcon\DiInterface $dependencyInjector])

Phalcon\Mvc\Micro constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the DependencyInjector container

public Phalcon\Mvc\Router\RouteInterface map (string $routePattern, callable $handler)

Maps a route to a handler without any HTTP method constraint

public Phalcon\Mvc\Router\RouteInterface get (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is GET

public Phalcon\Mvc\Router\RouteInterface post (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is POST

public Phalcon\Mvc\Router\RouteInterface put (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is PUT

public Phalcon\Mvc\Router\RouteInterface patch (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is PATCH

public Phalcon\Mvc\Router\RouteInterface head (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is HEAD

public Phalcon\Mvc\Router\RouteInterface delete (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is DELETE

public Phalcon\Mvc\Router\RouteInterface options (string $routePattern, callable $handler)

Maps a route to a handler that only matches if the HTTP method is OPTIONS

public mount (Phalcon\Mvc\Micro\CollectionInterface $collection)

Mounts a collection of handlers

public Phalcon\Mvc\Micro notFound (callable $handler)

Sets a handler that will be called when the router doesn't match any of the defined routes

public Phalcon\Mvc\Micro error (callable $handler)

Sets a handler that will be called when an exception is thrown handling the route

public getRouter ()

Returns the internal router used by the application

public Phalcon\Di\ServiceInterface setService (string $serviceName, mixed $definition, [boolean $shared])

Sets a service from the DI

public hasService (mixed $serviceName)

Checks if a service is registered in the DI

public object getService (string $serviceName)

Obtains a service from the DI

public mixed getSharedService (string $serviceName)

Obtains a shared service from the DI

public mixed handle ([string $uri])

Handle the whole request

public stop ()

Stops the middleware execution avoiding than other middlewares be executed

public setActiveHandler (callable $activeHandler)

Sets externally the handler that must be called by the matched route

public callable getActiveHandler ()

Return the handler that will be called for the matched route

public mixed getReturnedValue ()

Returns the value returned by the executed handler

public boolean offsetExists (string $alias)

Check if a service is registered in the internal services container using the array syntax

public offsetSet (string $alias, mixed $definition)

Allows to register a shared service in the internal services container using the array syntax

<?php

$app["request"] = new \Phalcon\Http\Request();

public mixed offsetGet (string $alias)

Allows to obtain a shared service in the internal services container using the array syntax

<?php

var_dump(
    $app["request"]
);

public offsetUnset (string $alias)

Removes a service from the internal services container using the array syntax

public Phalcon\Mvc\Micro before (callable $handler)

Appends a before middleware to be called before execute the route

public Phalcon\Mvc\Micro afterBinding (callable $handler)

Appends a afterBinding middleware to be called after model binding

public Phalcon\Mvc\Micro after (callable $handler)

Appends an 'after' middleware to be called after execute the route

public Phalcon\Mvc\Micro finish (callable $handler)

Appends a 'finish' middleware to be called when the request is finished

public getHandlers ()

Returns the internal handlers attached to the application

public getModelBinder ()

Gets model binder

public setModelBinder (Phalcon\Mvc\Model\BinderInterface $modelBinder, [mixed $cache])

Sets model binder

<?php

$micro = new Micro($di);
$micro->setModelBinder(new Binder(), 'cache');

public getBoundModels ()

Returns bound models from binder instance

public getDI () inherited from Phalcon\Di\Injectable

Returns the internal dependency injector

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager) inherited from Phalcon\Di\Injectable

Sets the event manager

public getEventsManager () inherited from Phalcon\Di\Injectable

Returns the internal event manager

public __get (mixed $propertyName) inherited from Phalcon\Di\Injectable

Magic method __get


Class Phalcon\Mvc\Micro\Collection

implements Phalcon\Mvc\Micro\CollectionInterface

Source on GitHub

Groups Micro-Mvc handlers as controllers

<?php

$app = new \Phalcon\Mvc\Micro();

$collection = new Collection();

$collection->setHandler(
    new PostsController()
);

$collection->get("/posts/edit/{id}", "edit");

$app->mount($collection);

Methods

protected _addMap (string | array $method, string $routePattern, mixed $handler, string $name)

Internal function to add a handler to the group

public setPrefix (mixed $prefix)

Sets a prefix for all routes added to the collection

public getPrefix ()

Returns the collection prefix if any

public array getHandlers ()

Returns the registered handlers

public Phalcon\Mvc\Micro\Collection setHandler (mixed $handler, [boolean $lazy])

Sets the main handler

public setLazy (mixed $lazy)

Sets if the main handler must be lazy loaded

public isLazy ()

Returns if the main handler must be lazy loaded

public mixed getHandler ()

Returns the main handler

public Phalcon\Mvc\Micro\Collection map (string $routePattern, callable $handler, [string $name])

Maps a route to a handler

public Phalcon\Mvc\Micro\Collection get (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is GET

public Phalcon\Mvc\Micro\Collection post (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is POST

public Phalcon\Mvc\Micro\Collection put (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is PUT

public Phalcon\Mvc\Micro\Collection patch (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is PATCH

public Phalcon\Mvc\Micro\Collection head (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is HEAD

public Phalcon\Mvc\Micro\Collection delete (string $routePattern, callable $handler, [string $name])

Maps a route to a handler that only matches if the HTTP method is DELETE

public Phalcon\Mvc\Micro\Collection options (string $routePattern, callable $handler, [mixed $name])

Maps a route to a handler that only matches if the HTTP method is OPTIONS


Interface Phalcon\Mvc\Micro\CollectionInterface

Source on GitHub

Methods

abstract public setPrefix (mixed $prefix)

...

abstract public getPrefix ()

...

abstract public getHandlers ()

...

abstract public setHandler (mixed $handler, [mixed $lazy])

...

abstract public setLazy (mixed $lazy)

...

abstract public isLazy ()

...

abstract public getHandler ()

...

abstract public map (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public get (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public post (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public put (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public patch (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public head (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public delete (mixed $routePattern, mixed $handler, [mixed $name])

...

abstract public options (mixed $routePattern, mixed $handler, [mixed $name])

...


Class Phalcon\Mvc\Micro\Exception

extends class Phalcon\Exception

implements Throwable

Source on GitHub

Methods

final private Exception __clone () inherited from Exception

Clone the exception

public __construct ([mixed $message], [mixed $code], [mixed $previous]) inherited from Exception

Exception constructor

public __wakeup () inherited from Exception

...

final public string getMessage () inherited from Exception

Gets the Exception message

final public int getCode () inherited from Exception

Gets the Exception code

final public string getFile () inherited from Exception

Gets the file in which the exception occurred

final public int getLine () inherited from Exception

Gets the line in which the exception occurred

final public array getTrace () inherited from Exception

Gets the stack trace

final public Exception getPrevious () inherited from Exception

Returns previous Exception

final public Exception getTraceAsString () inherited from Exception

Gets the stack trace as a string

public string __toString () inherited from Exception

String representation of the exception


Class Phalcon\Mvc\Micro\LazyLoader

Source on GitHub

Lazy-Load of handlers for Mvc\Micro using auto-loading

Methods

public getDefinition ()

...

public __construct (mixed $definition)

Phalcon\Mvc\Micro\LazyLoader constructor

public mixed __call (string $method, array $arguments)

Initializes the internal handler, calling functions on it

public mixed callMethod (string $method, array $arguments, [Phalcon\Mvc\Model\BinderInterface $modelBinder])

Calling __call method


Interface Phalcon\Mvc\Micro\MiddlewareInterface

Source on GitHub

Methods

abstract public call (Phalcon\Mvc\Micro $application)

...