I have an angular project which is my root website and having a Laravel project in the backend folder in public_html. I have two types of routes, /backend/api/** is my api routes which I want to redirect to /404 in angular project when user want to access directly in the browser but want to access via httpclient module in angular and I have /backend/admin/** which is my admin routes and I want to stay untouched and can be accessed via the browser. My problem is the ** route in the angular overlap all the laravel routes and redirects to /404. what is the best solution. should I change .htaccess file or what? would you please help me?
            Asked
            
        
        
            Active
            
        
            Viewed 141 times
        
    0
            
            
         
    
    
        Araz Shamsaddinlouy
        
- 381
- 2
- 13
- 
                    you can add custom header to angular `httpClient` then based on server (laravel can react) – Kamlesh Paul Apr 12 '21 at 07:16
- 
                    the request does not get to laravel controller due to ** route in angular – Araz Shamsaddinlouy Apr 12 '21 at 07:18
- 
                    means your not able to call api ? – Kamlesh Paul Apr 12 '21 at 07:22
- 
                    strangely i can call api but when i load that api route in browser it redirects to 404 – Araz Shamsaddinlouy Apr 12 '21 at 07:26
- 
                    oh then what is wrong with it ? – Kamlesh Paul Apr 12 '21 at 07:27
- 
                    even my admin routes redirect to 404. /backend/admin – Araz Shamsaddinlouy Apr 12 '21 at 07:28
- 
                    oh then remove that redirection and use custom header to redirect only api routes to angular – Kamlesh Paul Apr 12 '21 at 07:29
- 
                    would you please give me an example how is that work – Araz Shamsaddinlouy Apr 12 '21 at 07:30
1 Answers
0
            
            
        i don't know about angular so i took ref from this Adding a HTTP header to the Angular HttpClient doesn't send the header, why?
in angular
let headers = new HttpHeaders();
headers = headers.set('from', 'api');
then create a middleware
public function handle($request, Closure $next)
{
    if($request->header('from') != 'api'){
        return redirect('/'); // replace this with your url
    }
    return $next($request);
}
NOTE this is not exact solution but it is an idea
 
    
    
        Kamlesh Paul
        
- 11,778
- 2
- 20
- 33
- 
                    that is ok for api call and admin call but how about the other routes ... i mean the other non angular routes will not redirect to 404 ... since i need to remove 404 route from angular then doing that – Araz Shamsaddinlouy Apr 12 '21 at 07:38
-