Question

In example, I want to add additional methods to Request class, like getRequiered*($name) which will throw exception in case of missed param in request. I'd like to implement it like this:

class MySmartRequest extends Request {
    // ...
    function getIntRequired($name) {
        $res = $this->get($name, null);
        if (null === $res) { throw new Exception('Missed required param'); }
        return (int) $res;
    }
}

// ...

$app->before(function(Request $r) {
    return new MySmartRequest($r);
});

Is it possilbe?

Was it helpful?

Solution

Yes, it is possible (never actually done this, following is just a tip by reading the code).

You'll need to create a subclass of Silex\Application, and overwrite the run() function to something like this:

public function run(Request $request = null)
{
    if (null === $request) {
        $request = MySmartRequest::createFromGlobals();
    }

    $response = $this->handle($request);
    $response->send();
    $this->terminate($request, $response);
}

To avoid duplication, you can try this:

public function run(Request $request = null)
{
    if (null === $request) {
        $request = MySmartRequest::createFromGlobals();
    }

    parent::run($request); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top