Abdes
Abdes

Reputation: 986

Laravel: disallow direct access to Post method by users

I have a website which is developed in Laravel.

Problem:

I have a route method (POST)

Route::post('/profile/edit/save', 'ProfileController@save');

if I enter this url "mywebsite.com/profile/edit/save" I get an error enter image description here Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

my .env file:

 APP_NAME=Laravel
 APP_ENV=production
 APP_DEBUG=false
 APP_LOG_LEVEL=debug
 APP_URL=http://localhost

I get this error while running this code on my server,in Localhost I don't get any errors.

I have cleared the cache but it's not working

How can i solve this Problem?

Upvotes: 1

Views: 2111

Answers (2)

Abdes
Abdes

Reputation: 986

I have solved it by adding this in app\Exceptions\handler.php

    public function render($request, Exception $exception)
        {   

             if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
                 return redirect('/');
             }

        return parent::render($request, $exception);
    }

Upvotes: 2

Akbar Soft
Akbar Soft

Reputation: 1066

You cannot access to post url like you tried above You can only access by submitting form like this...

<form action="route('post_insert')" method="post">
  {{csrf_field()}}
   <input type="text" />
<input type="submit"/>
</form>

your route must include this name...

Route::post('/profile/save', 'ProfileController@save')->name('post_insert');

Upvotes: 1

Related Questions