Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mrs_Beginner's avatar

use same request in Web route and API route with different controllers but get same result in non validate requests

hi im using StoreImageRequest with below code:

class StoreImageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        return [
            'martyr_id'=>'required',
            'imgurl'=>'image|mimes:jpeg,bmp,png|max:2000|required',
        ];
    }
}

and web.php in routes folder is like below:

Route::post('recognize/save', 'HomeController@recognizefromsite');

and api.php in routes folder is like below:

Route::prefix('v1')->namespace('Api\v1')->group(function (){
    Route::get('/recognize','RecognizeController@index');
    Route::post('/recognize/fromapp', 'RecognizeController@recognizefromapp');

});

HomeController@recognizefromsite is like blow:

    public function recognizefromsite (RecognizeImageRequest $request)
{
        $path = request('imgurl')->store('images');
     return redirect('/success');
}

and RecognizeController@recognizefromapp is like below:

    public function recognizefromapp(Request $request){
$validate=$this->validate($request, [
            'imgurl'=>'image|mimes:jpeg,bmp,png,jpg|max:9000|required',
        ]);
        if ($validate==true){
            dd($validate);

        }else{
            return "we have problem here";
        }

}

and in HomeController of web.php route when i use it after successful upload return success upload page. its working fine

but when i use it in RecognizeController of API when image is validate its work fine but when image is not validate (for wrong type or size or some thing else) i should recive "we have problem here" message but this time mqain home page is return to client of api.

i think its from exceptions that im new in laravel and dont know them.

0 likes
3 replies
Dalma's avatar

Can you share the code in your routes files web.php and api.php along with the controller in question?

1 like
bestmomo's avatar
bestmomo
Best Answer
Level 52

From documentation :

If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user. In the case of a traditional HTTP request, a redirect response will be generated, while a JSON response will be sent for AJAX requests... if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.

1 like

Please or to participate in this conversation.