1. Go to this page and download the library: Download nin/phalcon-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nin / phalcon-middleware example snippets
composer
$container = new \Phalcon\Di\FactoryDefault();
$container->register(new \Nin\Middleware\ServiceProvider());
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
]
],
];
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);
}
}
new \Phalcon\Mvc\Router\Group([
'module' => 'backend',
'middleware' => 'web',
]);
class InvoicesController extends Controller
{
public function initialize()
{
$this->middleware->attach('auth');
}
public function editAction()
{
// OR: $this->middleware->attach('auth');
}
}
$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);
}
}
public function handle($request)
{
if ($this->getParam() !== 'admin') {
return $this->redirectToRoute('login');
}
return parent::handle($request);
}
public function handle($request)
{
if ($this->getParam() !== 'admin') {
return $this->redirectToUrl('/login');
}
return parent::handle($request);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.