When to use interrupt (404) in Laravel?

I want to create a registration check,

Until now, I planned that the My Users table should have a column confirmation_code

and a column confirmed

. The designated column has a default value of 0 (boolean false).

I create a new user from registration and assign a verification code to it.

Then I send them a link to the verification route that expects this verification code in the query string.

The method that handles the validation request will check if the query string parameter (confirm_code) exists.

My question is:

If there is no confirm_code, should abort (404) method be used? Or throw a custom exception?

public function verify($confirmation_code)
{
    if ( ! $confirmation_code ) {

        // abort(404)?
        // or,
        // throw new Exception?

      

Some feedback on when to use the interrupt method would also be appreciated.

+3


source to share


2 answers


There should be a pretty simple rule of thumb.

If the user can correct the missing field, enter the error in your view.

If the user is unable to correct the missing field, throw an exception. How you sign up is up to you, and you may or may not need to tell the user something "bad" depending on the context.

If the resource the user is looking for does not exist, or should not exist for the user for some reason, show a 404. The classic case is by going to route in /admin

without being logged in. You can use 401 as well, but this is misleading unless the route accepts the header WWW-Authenticate

in a subsequent request.



The essence:

If something went wrong due to the user doing something wrong, notify the user about it.

If something went wrong because things didn't go as expected, report your logs and (optionally) notify the user. Exceptions are great ways to encapsulate all of this.

If something doesn't exist or shouldn't be there, if the user doesn't change anything about himself or the request, show 404.

0


source


It is actually your choice to choose the type of error handling and error logging that you implement in your project.

As a tip, I will say that using custom exceptions for missing field exceptions. that's the best idea. You can read more about 404 error in this link:



error 404 .

Also refer to laravel documentation for more details on error logging.

+1


source







All Articles