1

I add session and session-ext packages to a Mezzio project.

After that I get an error:

Too few arguments to function Mezzio\Session\SessionMiddleware::__construct(), 0 passed in /var/www/mezzio/vendor/mezzio/mezzio/src/MiddlewareContainer.php on line 57 and exactly 1 expected

I find that mezzio-session uses Zend-session and try to install Zend-session but this gives me an error too:

  • mezzio/mezzio-session-ext is locked to version 1.12.0 and an update of this package was not requested.
  • mezzio/mezzio-session 1.6.0 conflicts with zendframework/zend-expressive-session 1.3.0.
  • mezzio/mezzio-session-ext 1.12.0 requires mezzio/mezzio-session ^1.4 -> satisfiable by mezzio/mezzio-session[1.6.0].
  • Root composer.json requires zendframework/zend-expressive-session ^1.3 -> satisfiable by zendframework/zend-expressive-session[1.3.0].

How can I use mezzio-session and -session-ext in a Mezzio project?

Update 1:

I get this error on a route where I have used SessionMiddleware::class in the route pipeline as described here:

use Mezzio\Authentication\OAuth2;
use Mezzio\Session\SessionMiddleware;

$app->route('/oauth2/authorize', [
    SessionMiddleware::class,

    OAuth2\AuthorizationMiddleware::class,

    // The following middleware is provided by your application (see below):
    App\OAuthAuthorizationMiddleware::class,

    OAuth2\AuthorizationHandler::class
], ['GET', 'POST']);

1 Answer 1

0

You should not be using any of the old Zend* packages. You should only need to run

composer require mezzio-session-ext

The *-ext package requires mezzio-session. There is no other required packages. Remove the Zend framework packages that you have installed.

  1. If you are not using the laminas component installer package you will need to edit /config/config.php and add an entries for both packages ConfigProvider classes so their autowiring will work.

  2. You will need to pipe the SessionMiddleware in pipeline.config $app->pipe(\Mezzio\Session\SessionMiddleware::class);

You can find more information along with examples here: https://docs.mezzio.dev/mezzio-session/middleware/#configuration

There is several ways to access the session data after the middleware is piped. Which method you use to retrieve the session data will be up to you. There is 4 ways (that I know of):

$session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

$session = $request->getAttribute(SessionInterface::class);

SessionRetrieval::fromRequest($request);

SessionRetrieval::fromRequestOrNull($request);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.