Question

I have a resource:

Route::resource('artists', 'ArtistsController');

For a particular url (domain.com/artists/{$id} or domain.com/artists/{$url_tag}), I can look at the individual page for a resource in the table artists. It is controlled by this function:

public function show($id)
    {

        if(!is_numeric($id)) {
                $results = DB::select('select * from artists where url_tag = ?', array($id));
                if(isset($results[0]->id) && !empty($results[0]->id)) {
                    $id = $results[0]->id;
                }
            }
            else {

                $artist = Artist::find($id);  

            }

        $artist = Artist::find($id);

        return View::make('artists.show', compact('artist'))
            ->with('fans', Fan::all())
            ->with('friendlikes', Fanartist::friend_likes())
            ->with('fan_likes', Fanartist::fan_likes());

    }

What I would like to do is have all urls that are visited where the {$id} or the {$url_tag} don't exist int he table, to be rerouted to another page. For instance, if I typed domain.com/artists/jujubeee, and jujubee doesn't exist in the table in the $url_tag column, I want it rerouted to another page.

Any ideas on how to do this?

Thank you.

Was it helpful?

Solution

In your show method you may use something like this:

public function show($id)
{
    $artist = Artist::find($id);
    if($artist) {
        return View::make('artists.show', compact('artist'))->with(...)
    }
    else {
        return View::make('errors.notfound')->withID($id);
    }
}

In your views folder create a folder named errors (if not present) and in this folder create a view named notfound.blade.php and in this view file you'll get the $id so you may show something useful with/without the id.

Alternatively, you may register a global NotFoundHttpException exception handler in your app/start/global.php file like this:

App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
    // Use $e->getMessage() to get the message from the object
    return View::make('errors.notfound')->with('exception', $e);
});

OTHER TIPS

To redirect to another page have a look at the redirect methods available on the responses page of the Laravel docs.

This is how I would go about doing it and note that you can also simplify your database queries using Eloquent:

public function show($id)
{

    if( ! is_numeric($id)) {
        // Select only the first result. 
        $artist = Arist::where('url_tag', $id)->first();
    }
    else {
        // Select by primary key
        $artist = Artist::find($id);  
    }

    // If no artist was found
    if( ! $artist) {
        // Redirect to a different page.
        return Redirect::to('path/to/user/not/found');
    }

    return View::make('artists.show', compact('artist'))
        ->with('fans', Fan::all())
        ->with('friendlikes', Fanartist::friend_likes())
        ->with('fan_likes', Fanartist::fan_likes());

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top