Silex Setting Middleware to a ControllerCollection
Date : March 29 2020, 07:55 AM
help you fix your problem I want to do something like this: , you can do $controllers = $app["controllers_factory"];
$controllers->before(function(Request $request){});
$app['callback'] = $app->protect(function(){});
$controllers->before($app["callback"]);
|
How to pass arguments from the request to controller method with the silex method get()?
Date : March 29 2020, 07:55 AM
Hope that helps I try Silex Framework, and I may want to pass arguments to a controller method. I have this, like in the doc : , Just add projetName as parameter to the method: $app->get('/projet/{projetName}', 'App\Controller\Projet::single')
->bind('single.projet');
class Projet
{
public function single(Application $app, $projetName)
{
// do anything with $projetName
}
}
|
Silex traits for swiftmailer. Fatal error: Call to undefined method Silex\Application::mail()
Date : March 29 2020, 07:55 AM
With these it helps You need to create a custom Application class which extends \Silex\Application and uses that trait. Assuming a base project tree as: project/
|
|_app/
|
|_src/
|
|_vendor/
|
|_web/
// src/WL/App.php
namespace WL;
class App extends \Silex\Application
{
use \Silex\Application\SwiftmailerTrait;
// add some other trait
// even custom methods or traits
}
// app/bootstrap.php
$app = new \WL\App();
// configure it, register controllers and services, ...
// or import them
foreach (glob(__DIR__ . "/../src/WL/Controller/*.php") as $controllers_provider) {
include_once $controllers_provider;
}
return $app;
// src/Wl/Controller/blog.php
use Symfony\Component\HttpFoundation\Request;
/** @var \Silex\ControllerCollection $blog */
$blog = $app['controllers_factory'];
// define some routes
$blog->post('/send-mail', function (Request $request, \WL\App $app)
{
// Now this application passed to your controller is an
// instance of custom \App which has the trait you want
// in contrary with the default \Silex\Application
$app->mail(...
}
$app->mount('/blog', $blog);
// web/index.php
// define autoloading
// customize debug and server parameters
$app = require_once '../app/bootstrap.php';
$app->run();
|
Silex 2: Get route in before middleware
Tag : php , By : user121501
Date : March 29 2020, 07:55 AM
it fixes the issue I have defined a before middleware in my Silex 2.0 application, and I would like to know what route is being processed. , $request->getPathInfo() should work
|
Silex application before middleware cancel route
Tag : php , By : Nosayaba
Date : March 29 2020, 07:55 AM
wish of those help If you want to cancel your request immediately and return a 400 response make use of exceptions. In your case the user is unauthorized so something like 401 would fit. use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpFoundation\Response;
$app->match('/', function () use ($app) {
$app->before(function (Request $request) {
$loggedIn= false;
if (!$loggedIn) {
throw new UnauthorizedHttpException(null,'unauthorized',null,Response::HTTP_UNAUTHORIZED);
}
});
});
$app->error(function (\Exception $e, Request $request, $code) {
$message = strlen($e->getMessage()) > 0 ? $e->getMessage() : null;
switch ($code) {
case Response::HTTP_UNAUTHORIZED:
$response = new Response($message, Response::HTTP_UNAUTHORIZED);
break;
default:
$response = new Response($message, Response::HTTP_NOT_FOUND);
break;
}
return $response;
});
|