Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Central callback/hook before route response is called #405

Open
felixWackernagel opened this issue Feb 14, 2020 · 1 comment
Open

Central callback/hook before route response is called #405

felixWackernagel opened this issue Feb 14, 2020 · 1 comment

Comments

@felixWackernagel
Copy link

Each of my routes start with the locale.

$klein->respond( '/', function ($request, $response, $service, $app) {
    $response->redirect( '/en/' );
});
$klein->with('/[*:locale]', function () use ($klein) {
    $klein->respond( 'GET', '/', [HomeController::class, 'get'] );
    $klein->respond( 'GET', '/foo', [FooController::class, 'get']);
});

For rendering i used twig and all information for twig is stored inside a service array called context.

$klein->respond( function ( $request, $response, $service, $app ) {
    $app->context = [
        'pageTitle' => 'Demo Site',
	'locale' => $request->locale
    ];
});

But at time of context initialization is locale null. When my callable of the respond-function is called only then is $request-locale available. I tried different approaches but everytime is locale not available.

@rekcuFniarB
Copy link

rekcuFniarB commented Feb 29, 2020

I'd try

$klein->respond( function ( $request, $response, $service, $app ) {
    $app->register('context', function() use ($request) {
        // Lazy  init
        return [
            'pageTitle' => 'Demo Site',
	    'locale' => $request->locale
        ];
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants