Skip to content
Advertisement

Passing a Slim 3 Exception response to an Angular application

I’m creating an authentication system that uses Slim 3 PHP on the back-end and Angular on the front-end. So far I’ve managed to have it so a user can fill out a form on the frontend and send a post request to the back-end to insert that data into a database table.

Angular Register Component:

JavaScript

Angular Authentication Service:

JavaScript

But on the back-end I have some form validation that produces some messages when say a password is less than 8 characters, which is in the form of a class that extends PHP’s LogicException class.

JavaScript

This InvalidFormData is thrown when validation fails in the constructor of the Password class which is where validation takes place for passwords:

JavaScript

Then finally in the controller class we try to register a user but if we catch an InvalidFormData exception we extract the produced message.

JavaScript

Question:

What I would like to do is pass this message back to the Angular front-end and display it on the form so a user is able to see why they failed validation.

I’ve read through Slim 3’s response documentation but I’m not sure If I would have to send this message back as a Slim response, and if so how would I capture this within Angular? My first thought was I might need a get request within angular to get the message, but I’m sure there is a way to do it within the original post request.

I have read some of the Angular documentation and it seems I need to do something in the subscribe method but I’m not really sure what.

Hopefully someone could show me some some code examples of this process. How would I create a Slim 3 response containing the message and how might I capture this in Angular?

Advertisement

Answer

set in src/routes.php:

JavaScript

The response layout was inspired on Instagram API (https://www.instagram.com/developer/endpoints/)

I have no contact with Angular for some years, but I think you have to do something like postUserDetails( params, here ).map(res=> res.json()) or another “responseHandler” method from the version of Angular you’re using.

I’m use to do it with fetch API (https://developer.mozilla.org/pt-BR/docs/Web/API/Fetch_API/Using_Fetch) or axios (https://github.com/axios/axios) in my projects

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement