All the APIs the laravel middleware is not working - laravel

I am using an auth middleware in laravel. If I am calling the routes which does not have middleware('auth:api') then it is working fine But routes under middleware('auth:api') not working.
I have tried to update htaccess file and tried approx all the solutions, but no results.
My Route code is -
Route::group(['middleware' => 'auth:api'], function(){
Route::post('unregistered', 'API\InterestController#getUnregistered');
Route::post('new-campaign-listing','API\CampaignController#new_campaign_list');
Route::post('registered-campaign-listing','API\CampaignController#registered_campaign_list');
Route::get('past-campaign-listing','API\CampaignController#past_campaign_list');
Route::get('getcampaignhistory','API\CampaignController#getCampaignhistory');
Route::post('register-for-campaign', 'API\CampaignController#register');
Route::post('top-campaign-photos', 'API\CampaignController#topPhotos');
Route::get('user-detail', 'API\PassportController#getdetails');
//update profile
Route::post('update-profile', 'API\PassportController#update_profile');
//update profile
Route::post('shared-post-data','API\CampaignController#shared_post_data');
Route::post('vip-campaigns','API\CampaignController#vip_campaigns');
Route::post('vip_client','API\CampaignController#vip_client');
Route::post('add_user_interests', 'API\InterestController#add_user_interests');
Route::get('user-interests', 'API\InterestController#user_interests');
Route::post('update-user-interests', 'API\InterestController#update_user_interest');
//polls
Route::post('polls-for-vote', 'API\PollController#pollsForVote');
Route::post('vote', 'API\PollController#vote');
Route::post('contact-us', 'API\PassportController#contactmail');
Route::get('campaignpostimage','API\PassportController#campaignpostimage');
Route::post('firebase-token', 'API\PassportController#get_token');
//notifications
Route::post('notifications', 'API\InterestController#notifcations');
Route::post('unread_notifications', 'API\InterestController#unreadNotifications');
Route::get('coupons','API\CampaignController#coupons_list');
Route::post('redeem','API\CampaignController#redeem_points');
Route::post('campaign-detail','API\CampaignController#campaign_detail');
Route::get('rewards_history','API\CampaignController#rewards_history');
});
My function in passportcontroller is :
public function getDetails()
{
dd("hello");
}
My htaccess file code is :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# RewriteCond %{HTTP:Authorization} ^(.*)
# RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Even dd in controller is not printing. means API URL is not working

What is the error message?
Note:You should put the routes inside api.php

Please provide more info about how you are consuming your API?
if you are consuming the API's from different domain (Cross domain) than you will face the CORS issue, in that case you need to add the following package to your laravel application
and if you are on same domain tha you can follow the
documentation.
In my case this
git hub reported issue solved my problem

Related

Is there any changes required in the .htaccess file to acces the API routes?

I have added a few routes to the api.php file. These routes are working perfectly in the localhost environment.
http://localhost/api/v1/events
gives desired output but when uploaded to the production/remote server I cannot access it using the domain name.
http://domainname/api/v1/events
throws an 404 error. I am using the same .htaccess files in the localhost as well as in the prod/remote server. I am not sure why is it not working correctly in the prod/remote server.
By the way, I have web routes which works fine in both the environments. My .htaccess file is copied below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Modified .htaccess as per the answer but it is not working for me :(
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# API
RewriteRule ^api/(.*) api.php/$1 [L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Tried this as well.
RewriteRule ^api/(.*) routes/api.php/$1 [L]
Can anyone help by advising what is wrong?
My root folder
the folder in which api.php is available
The confusing part about this is that you say it works "perfectly in the localhost", yet you are using the same .htaccess file in both environments. This cannot work on localhost unless you have some other directives somewhere since there is nothing that would rewrite such a request to /api.php.
To enable this, and pass the additional path information as path-info to api.php, then you would need to add something like the following after the # Handle Authorization Header rule:
# API
RewriteRule ^api/(.*) api.php/$1 [L]
This would internally rewrite a request of the form /api/v1/events to /api.php/v1/events, allowing api.php to read /v1/events from the $_SERVER['PATH_INFO'] superglobal.
Without this directive (and with MultiViews being disabled) a request of the form /api/v1/events would be routed through index.php instead where I assume the "route" is not defined, hence the 404.
UPDATE#1: No my /api.php is under the same directory which has web.php, that is routes
In that case, you need to modify the above rule to read:
RewriteRule ^api/(.*) routes/api.php/$1 [L]
UPDATE#2: from your updated URL structure, it seems that /routes is actually under /myprojectfolder in the document root (where the .htaccess and index.php files are located), so you would need to modify the above to account for this. For example:
RewriteRule ^api/(.*) myprojectfolder/routes/api.php/$1 [L]
However, it seems that a direct request for /myprojectfolder/routes/api.php/v1/events does not work anyway, so passing this as path-info would not seem to be the correct course of action to begin with.

Laravel url method post not point to project folder?

why my laravel url not working when method is post
example the url api must be : http://localhost/PROJECTNAME/api/loginWeb
but when i click post button it always direct to http://localhost/api/loginWeb
but when method is get this okay. i use xampp
here is my htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When we're using session based laravel setup, all the route generator functions(url(), route()) use hostname http://localhost as root url, not http://localhost/PROJECTNAME. To resolve this issue please do following changes in laravel app.
Define APP_URL in .env file as APP_URL="http://localhost/PROJECTNAME"
Go to RouteServiceProvider which is located at app/Providers/RouteServiceProvider and force laravel to use APP_URL as root url for your app.
public function boot()
{
parent::boot();
// Add following lines to force laravel to use APP_URL as root url for the app.
$strBaseURL = $this->app['url'];
$strBaseURL->forceRootUrl(config('app.url'));
}
Same issue occurred to me when I used same domain and a suburl to identify different projects.
Linux: Nginx serves multiple apps with two different locations
Windows: Multiple Laravel Applications Using Nginx - Windows

Laravel .htaccess configuration for excluding POST endpoint from https

In my Laravel app i have a POST endpoint in which Arduino sends some data. I installed SSL and tried communication again but with no result (redirect). I want to exclude only this specific endpoint from https. I tried different approaches and code that found with no result.
e.g. Route endpoint: /api/measures
.htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thankfully i found a solution following a different way than changing .htaccess. With the help of Laravel i applied a middleware that redirects all web and api requests to secure requests and excluded the only endpoint that takes the request from Arduino client (can't handle ssl).
Answer that helped me Laravel 5 - redirect to HTTPS

laravel ERR_TOO_MANY_REDIRECTS after adding SSL

so i am using cloudways with digitalocean and now trying to add letsencrypt into my laravel 5.4 web app...
and according to tech support in cloudways they say i need to modify my .htaccess to be like this
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
but after that i got
ERR_TOO_MANY_REDIRECTS
what's gone wrong? cloudways tech support can't give any solution either... so i kinda stuck in here
i also do some research before and read that i need to add
If(env(' APP_ENV') !== 'local') { $url->forceSchema('https'); }
into app/Providers/AppServiceProvider.php in boot function to make all request to be https....
but still no luck...
okay so i already got the solution, i forgot that my site also have cloudflare cdn and in crypto part it set SSL to Full mode, just change it to flexible one and everything works great....

Laravel routing triggers 500 error on subdomain

I am trying to put my laravel project on a live server. It is supposed to run on subdomain http://api.domain.com. The problem is it shows 500 internal error on any route except:
Route::get('/', function () {
return view('welcome');
});
Folder structure:
/
--laravel
--subdoms
----api
--www
When I put content of public folder of laravel project into www folder it runs on domain.com perfectly, database, authenticating, routing, it works.
When I put that content into api folder and I visit api.domain.com it loads welcome view. But when I add another route:
Route::get('testing', function() {
return "Tested.";
});
This shows 500 internal error.
Thank you for any feedback.
Edit: my .htaccess file in public:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Resources