App::error() isn't for creating exceptions, it's for catching them. The best way to handle that would probably be in your controller, though I should caution that you probably don't need your own custom exception for that case. I think it's perfectly exceptable for article/asdf to return a 404.
thepsion5 said:
App::error() isn't for creating exceptions, it's for catching them. The best way to handle that would probably be in your controller, though I should caution that you probably don't need your own custom exception for that case. I think it's perfectly exceptable for article/asdf to return a 404.
But I want to create an error to JSON data. my app have 2 zone 1 for site, another for API.
In my Controller when user route to /article/1234 (there not found in database) I create an error JSON.
but when user route to /article/asdf (there not found on route regex) it return html error. I think it doesn't make sense.
Is there is a way to handle that?
If you return a 404 response there wont be any html error. The call to your api just wont success. There is an html error because of the uncatched exception.
You should catch the route not found exception and the model not found exception and return a 404 reponse.
pmall said:
If you return a 404 response there wont be any html error. The call to your api just wont success. There is an html error because of the uncatched exception.
You should catch the route not found exception and the model not found exception and return a 404 reponse.
If I catch the route not found exception, All my 404 error page is return to JSON.
but I want to catch the where('id', '[0-9]+');. Is that can be ?
I think another way to do that with beforeFilter()
but have question about Is there a way to filter the Input::get(); with regex ?
iClosedz said:
I think another way to do that with beforeFilter()
but have question about Is there a way to filter the Input::get(); with regex ? You MIGHT be able to use a filter for that too, I believe the filter function can be written like so:
Route::filter('log', function($route, $request)
{
//do stuff
});
The $request parameter is an object that contains the input, so you might be able to modify it there. But I don't know if laravel uses the same request object in the filter as it does in the input facade, so if not then your changes never get passed to the controller.
thepsion5 said:
iClosedz said:
I think another way to do that with beforeFilter()
but have question about Is there a way to filter the Input::get(); with regex ? You MIGHT be able to use a filter for that too, I believe the filter function can be written like so:
Route::filter('log', function($route, $request) { //do stuff });
The $request parameter is an object that contains the input, so you might be able to modify it there. But I don't know if laravel uses the same request object in the filter as it does in the input facade, so if not then your changes never get passed to the controller.
OK. I got it Thank you very much thepsion5.
my solution
on my filter.php
Route::filter('number', function($route, $request)
{
foreach ($route->parameters() as $value){
//d(preg_match('/^[1-9][0-9]*$/', $value));
if(!ctype_digit($value)) throw new Acme\Exceptions\MyNotFoundException('Not Found!');
}
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community