In previous example we already had access to database and user model which allow us to use user model globally as model to create user login. To create authentication in Slim, we have to create authentication controller, View and create a form where user can sign up with account and last we have to insert new user login record in our database.
1. Create user login View
Inside the resources/view create new folder called auth and add new file signup.twig
Step 2: Define User Model in app/Models/User.php
Step 3: Create authController
Inside the app/Controllers folder add new folder called Auth and add new file called AuthController.php. In AuthController.php we have two methods
1. getSignUp ( to render the view template auth/signup.twig file)
This method is called when we sent get request to http://localhost/testSlim/public/auth/signup
2. postSignUp (This is called when we submit the form)
This method is called when we submit the login form, it will create new user and insert new user into our database table user.
Add following code
For the password we are using php password hashing API, first argument is data that we want to hash and second argument is algorithm we want to use.
Step 3: Define route for signup get and post methods, add following code in app/routes.php
We can attach name to route for controller, so we can used is as reference in our code of view and controller.
Step 4: Define AuthController in our bootstrap/app.php
Step 5: Insert new user account in our database table