Zend Framework 3: Zend Expressive

Ref: https://docs.zendframework.com/zend-expressive/
https://docs.zendframework.com/zend-expressive/v3/getting-started/quick-start/
Create Login functionality in Expressive 3

Install via composer:

Then go to the project root directory:

Run the project

Then open it on your browser: http://localhost:8080/
NOTE: Make sure no other service running port 8080. If it exist, please stop it. In my composer nginx running port 8080. So I need to stop nginx (sudo service nginx stop).

Create Login
I need to install a few components:

Ok. I want to put the authentication if any visitor want to visit the homepage so he/she need to login first. I need to create a route for this. so modify config/routes.php file like this:

comment the default home route then add a new route like above. If we refresh our browser for the homepage, it’d show this error:

we need to register ‘Zend\Expressive\Authentication\AuthenticationInterface’ under ‘factories’ config in config/autoload/dependencies.global.php.:

If we refresh our browser again, it’d show another error:

So I modify again config/autoload/dependencies.global.php to add ‘Zend\Expressive\Authentication\UserRepositoryInterface’ class in ‘aliases’ config:

When I refresh the browser again. This error would be appear:

So I need to put my database credentials to connect zend expressive to mysql database in config/autoload/local.php file (if this file not exist, please create one):

Please create a new database ‘expressive’ and a new table ‘users’ first. The users table consist of only two fields now. There are username (varchar 100) and password (varchar 100). I put ‘redirect’ value to ‘/login’ so it’d redirect the homepage to login page. Make sure the credentials is correct. If not you’d get an error like this:

Then I refresh my browser again. Another error is coming up:

Here we need ‘Zend\Expressive\Session\SessionMiddleware’ class before routing middleware. So add it in config/pipeline.php:

We fixed those errors until here. When we refresh our browser, we’d get the login page. But the content is showing 404 because we haven’t defined the login page!Create the login form. Add a new file ‘LoginForm.php’ in ‘src/App/Form’ directory. The login form will show username and password fields. The post the form, I’ll put a submit button:

then create a login page handler with inject it with login form with the following factory in src/App/Handler/LoginPageFactory.php:

The LoginPageHandler itself can be initialized in src/App/Handler/LoginPageHandler.php:

Above, we redirect to ‘/’ page when there is a session data as it already authenticated check. We are going to add authentication process next.

The Login form can be as simple as the following in templates/app/login-page.phtml

We can register the LoginPageHandler at App\ConfigProvider::getDependencies() config in src/App/ConfigProvider.php

The routing can be registered as follows with add \Zend\Expressive\Authentication\AuthenticationMiddleware::class for next middleware in config/routes.php:

Until this, when I refresh the browser, I got this error and can’t continue anymore. I still dont know why the error is happened:

I need learn Zend Expressive more!