Route::post('news/is_display', 'NewsController@is_display');
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
namespace App\Http\Middleware;
class OnlyAjax
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        if (!$request->ajax()) {
            return response('Forbidden.', 404);
        }
        
        return $next($request);
    }
}
麻煩各位了,謝謝。
我對 laravel 只有略懂的程度
說錯不要罵我
1 .
現在有一個route限制為ajax且是post才能訪問
是 因為此 route 的 method 為 post
2 .
透過ajax post方式訪問是正常的,但直接網址訪問這個route會出現error 405
你所謂的網址直接訪問應該是指 GET
那這樣的話很正常
因為此 route 只有定義 post
所以當然會 return 405 (method not allow)
也就是不支援的 method
所以你的問題是什麼?
你是想要
1 .
不想讓 user 看到預設的那個 error page
2 .
想把 method not allow 的 exception
全部 redirect 404
1 .
根據 官方文件
想自訂義 http error page
例如 405
可以在以下路徑建立一個 405.blade.php
resources/views/errors/405.blade.php
這樣一旦 response code 為 405
就會回傳此頁
2 .
想把 method not allow 的 exception
全部 redirect 404 的話
根據這篇
在 App\Exception handler.php 的 render function
加上
    if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
      return abort('404');
    }
這樣所有的 http request exception
就會全部 response 404
標題要的
跟你說的狀況不是符合嗎
"laravel route設定post後如何避免用戶直接訪問此url"
內文提到post正常get不行
使用網址直接訪問就是get阿
還是你說的避免直接訪問是要自動跳轉404page