Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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 ?

Last updated 1 year ago.
0

I think another way to do that with beforeFilter()

but have question about Is there a way to filter the Input::get(); with regex ?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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!');
    }
});
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

iClosedz iclosedz Joined 10 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.