1

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?

Shreeraj
  • 758
  • 1
  • 9
  • 27
Abdes
  • 926
  • 1
  • 15
  • 27
  • If you want to access through the browser, you need to declare the route with GET method instead POST. – JSLirola Aug 25 '18 at 13:37
  • Maybe you have not understood my problem. if a user or a hacker on my website sees the source code of the form and may be he wants to see the url where the data will be posted, when he will type this url in the browser he will receive this page: https://i.stack.imgur.com/z9Fl8.png – Abdes Aug 25 '18 at 17:05
  • 1
    Check this solution: https://laravel.io/forum/10-14-2014-stack-trace-for-error-methodnotallowedhttpexception – JSLirola Aug 25 '18 at 20:22
  • thanks for your reply i found another solution you can see my answer – Abdes Aug 26 '18 at 09:33

2 Answers2

2

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);
    }
Abdes
  • 926
  • 1
  • 15
  • 27
1

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');
Akbar Soft
  • 1,028
  • 10
  • 19