简体   繁体   中英

How to detect missing parameter route and show relevant message

Working on Laravel 4, I created following route and able to fetch ID:

Route::get('details/{ID}', 'Exchange@details');

Issue is when I miss ID in route then it shows: NotFoundHttpException error. I want to avoid this. How do I proceed? Even in global.php I added following but did not work:

App::missing(function($exception)
{
    print "Not Found";
//  return Response::make(
//      View::make('errors/404')
//  , 404);
});

Controller Method

public function details($exchangeID)
        {
            echo "Print Details";
            echo $exchangeID;
            if(intval($exchangeID) == 0)
            {
                throw new NotFoundException;
            }
        }

Update: Recommendation of redirecting to 404 works but I am more interested to detect missing ID and load my Own View with proper message information instead of showing plain 404 page.

Try like this :

App::missing(function($exception)
{
    return View::make('errors.404');
});

The view is located in views/errors/404.blade.php

I would try something like this

# file routes.php
Route::get('{ID?}', 'SomeController@getDetails')



# file SomeController.php
function getDetails($ID=null) {
    if empty($ID) {
        return Redirect::to('PREVIOUS PAGE')
            ->withInput()
            ->with('error', 'invalid value');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM