Giter Club home page Giter Club logo

phalcon-middleware's Introduction

Phalcon 5 Middleware

Packagist License Latest Stable Version Total Downloads

This is a package to integrate middleware with Phalcon 5. Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application.

Installation:

Require this package with composer.

composer require nin/phalcon-middleware 

Register a Provider in index.php

$container = new \Phalcon\Di\FactoryDefault();
$container->register(new \Nin\Middleware\ServiceProvider());

OR app/config/providers.php

return [
    \Nin\Middleware\ServiceProvider::class,
];   

Usage:

Config:

Make config file config/middleware.php

<?php

return [
    // The application's global HTTP middleware stack
    // These middleware are run during every request to your application.
    'middleware_global' => [
         \App\Middleware\TrimStrings::class
    ],
    // The application's route middleware groups.
    'middleware_groups' => [
        'web' => [
            \App\Middleware\StartSession::class
        ],
        'api' => [
            \App\Middleware\ApiMiddleware::class,
            \App\Middleware\AdminApiMiddleware::class
        ]
    ],
];

Make Middleware Class:

<?php

namespace App\Middleware;

use Nin\Middleware\Middleware;

class TestMiddleware extends Middleware
{
    public function handle($request)
    {
        if ($request->get('token') !== 'valid-token') {
            throw new \Exception('Token invalid.');
        }
        return parent::handle($request);
    }
}

Assigning Middleware To Routes:

use Phalcon\Mvc\Router;

$router = new Router();
$router->addGet(
    '/invoices/edit/{id}',
    [
        'controller' => 'invoices',
        'action' => 'edit',
        'middleware' => ['web', 'auth'],
    ]
);

For groups

new \Phalcon\Mvc\Router\Group([
    'module' => 'backend',
    'middleware' => 'web',
]);

Assigning Middleware To Controller:

class InvoicesController extends Controller
{
    public function initialize()
    {
        $this->middleware->attach('auth');
    }

    public function editAction()
    {
    // OR:  $this->middleware->attach('auth');
    }

}

Middleware Parameters:

$this->middleware->attach( ['web', 'auth:admin']);

class AuthMiddleware  extends \Nin\Middleware\Middleware
{
    public function handle($request)
    {
        $param = $this->getParam();
        if ($param !== 'admin') {
            throw new \Exception('Authenticate Fail.');
        }
        return parent::handle($request);
    }
}

Redirecting:

Redirect to route:

public function handle($request)
{
    if ($this->getParam() !== 'admin') {
        return $this->redirectToRoute('login');
    }
    return parent::handle($request);
}

Redirect to url:

public function handle($request)
{
    if ($this->getParam() !== 'admin') {
        return $this->redirectToUrl('/login');
    }
    return parent::handle($request);
}

phalcon-middleware's People

Contributors

ninhnguyen22 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.