Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 3381
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
24条回答
  •  庸人自扰
    2020-11-22 02:00

    I have this simple solution I have been using and its works.

    In App/Exceptions/Handler.php

    Add this at top:

    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    

    Then inside the render method

    public function render($request, Exception $exception)
    {
        .......
    
           if ($exception instanceof NotFoundHttpException){
    
            $segment = $request->segments();
    
            //eg. http://site.dev/member/profile
            //module => member
            // view => member.index
            //where member.index is the root of your angular app could be anything :)
            if(head($segment) != 'api' && $module = $segment[0]){
                return response(view("$module.index"), 404);
            }
    
            return response()->fail('not_found', $exception->getCode());
    
        }
        .......
    
         return parent::render($request, $exception);
    }
    

提交回复
热议问题