Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:52+00:00 2026-06-13T06:35:52+00:00

In a Laravel controller I have a couple of methods that all begin by

  • 0

In a Laravel controller I have a couple of methods that all begin by fetching a database record, then after checking if data has been found either continue by rendering a view, or, in case of no data, go to a 404 page.

Here’s an example:

<?php

function get_show_user($id)
{
    $user = static::get_user($user_id);
    if (!$user) {
        return Response::error('404', static::$some_common_error404_message);
    }
    return View::make('users.show_readonly_user_data')->with('user', $user);
}

function get_edit_user($id)
{
    $user = static::get_user($user_id);
    if (!$user) {
        return Response::error('404', static::$some_common_error404_message);
    }

    return View::make('users.display_edit_user_form')->with('user', $user);
}

I’m repeating the entire if (!$user) statement in these methods, even if they all do the same thing.

I’d rather like to do something like this:

function get_show_user($id)
{
    $user = Users::find($id);
    static::go404_if_null($user);
    return View::make('users.show_readonly_user_data')->with('user', $user);
}

function get_edit_user($id)
{
    $user = Users::find($id);
    static::go404_if_null($user);
    return View::make('users.display_edit_user_form')->with('user', $user);
}

What would be the best way to implement such a DRY feature?

Obviously a simple return Response::error('404') would not work in the common existence checker method, since it would only return from that method.

It seems that an Event::fire('404') is not ideal either since it does not terminate the method it has been triggered in.

Perhaps using an exception would be required here, but I’m unsure about this, or how it should be done in Laravel. Where should I catch a 404 Exception of a controller?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:35:53+00:00Added an answer on June 13, 2026 at 6:35 am

    I think the best way to approach this would be a before filter on your controller.

    public static $require_user = array(
        'edit_user',
        'show_user',
    );
    
    public function before()
    {
        $route = Request::route();
        if ( in_array( $route->controller_action, static::$require_user ) )
        {
            $this->user = User::find( $route->parameters[0] );
            if ( is_null($this->user) ) return Response::error('404');
        }
    }
    

    Before filters get called after your controller is constructed but before the method is called. If a before filter returns anything other than null, the method is not called and thus execution is stopped.

    Here we obtain the current route being executed so we can check which method is going to be called against our array $require_user. This lets us use methods that don’t require a user id, such as login.

    Then we retrieve the user instance, getting the id from what would have been passed to the method. You should probably add more error handling here.

    Lastly, we check if the user returned was null, meaning not found. If that is the case we return a 404 response, stopping execution of the method.

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After much research, I have found that Laravel is very nice. I have a
I have just recently started working with Laravel. Great framework so far! However I
I'm using Laravel localization to provide two different languages. I've got all the path
I have just got started with laravel v3 and am trying to wrap my
I have just started playing with laravel haveing come from codeigniter and I am
In laravel for showing all error messages at once i use the following code
Hi I am using Laravel PHP framework on CentOS 6.3. I have PostgreSQL 9.1
When trying to use Laravel's Auth class, I see that it always fails because
I'm just learning Laravel, and have a working migration file creating a users table.
I have the following issue when trying to install Laravel http://laravel.com Any help, would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.