some laravel route not changing to https - laravel

There are some my route not going to HTTPS in my Javascript code
loadSettingTable("{{ route('setting.table.data.datatable') }}");
they become like this, so my datatable can't be load because mixed content.
loadSettingTable("http://*****.com/admin/data-setting-table/datatable");
but my css and JS file already become HTTPS.
I already give this on my AppServiceProvider and ENV
//AppServiceProvider.php on Boot
if(env('REDIRECT_HTTPS')) {
$url->formatScheme('https');
URL::forceScheme('https');
}
//.ENV
REDIRECT_HTTPS=true
But some of it work fine with this code, why my code with route name not giving https?
loadTableRealCommSumAgent("/agent/commissions-summary-member-ajax");

I found the error myself, but don't really know why this error happens on my production server not staging server. I remove $url->formatScheme('https'); because it giving some error on production but not always and it's fixed my problem.

Related

Laravel - 404 on some routes - only on local (Sail)

Pulling my hair out with this one.
I have an API that is working completly in both staging and production environments but recently has started return some 404 on two of the routes locally, and I cant work out why. (No changes have been made to the site locally).
Below is the affected route group:
Route::group(['middleware' => ['api.auth']], function () {
Route::post('role_update', 'App\Http\Controllers\MobileApi#role_update');
Route::post('request_items', 'App\Http\Controller\MobileApi#request_items');
Route::post('download_log', 'App\Http\Controllers\MobileApi#download_log');
Route::post('log_time', 'App\Http\Controllers\MobileApi#log_time');
Route::post('log_search', 'App\Http\Controllers\MobileApi#log_search');
});
The role_update & log_time routes return 404 locally and run perfectly in production and in the staging environment.
All of these routes are shown as generated on route:list.
I have run route:clear & route:cache & route:optimize to no beneficial effect.
I know that the methods are rechable on the controller and that the middleware isnt the issue as if I use one of the working routes URI's to call one of the non working route methods it works. For example, if I switch things around so that a post to download_log actually points to the role_update method the 404 disappears.
Interestingly I cannot change the URI's of any of these routes or they immediatly start throwing 404's even when confirmed that they are registered in route:list. I also can't add any new routes as they all throw 404's.
This makes me suspicious that the routes are being cached and not overridden somewhere?
My local environment is laravel sail - there has been no updates between this working and not working.
Laravel version - 8.30.1
Any help with this would be greatly appretiated.
Thanks
N.B - we have also just noticed that the role_update route is actually returning a 404 on the production and staging servers but only from our android application - its working on both iOS and postman. Very odd.
this problem is occur with me. if you update laravel may solve
composer install
composer update
For anyone finding this - I am the issue - I very stupidly forgot one of the variables required to access the API - to add to my stupidity, during testing I had the API throwing 404's if that variable was missing.
The reason it was working on some environments and not others is that my postman requests had the required variable in them just by chance! Obviously this would usually throw and error that I could work out be I had forced it to be 404's during development.
Sorry for any time taken thinking about this!

Laravel forcing Http for asssets

this is a little bit strange because most of the questions here wanted to force https.
While learning AWS elastic beanstalk. I am hosting a laravel site there. Everything is fine, except that none of my javascripts and css files are being loaded.
If have referenced them in the blade view as :
<script src="{{asset('assets/backend/plugins/jquery/jquery.min.js')}}"></script>
First thing I tried was looking into the file/folder permissions in the root of my project by SSHing into EC2 instance. Didn't work even when I set the permission to public folder to 777.
Later I found out that, the site's main page url was http while all the assets url were 'https'.
I dont want to get into the SSL certificates things just yet, if it is possible.
Is there anyway I can have my assets url be forced to Http only?
Please forgive my naiveity. Any help would be appreciated.
This usually happens if your site is for example behind an reverse proxy, As the URL helper facade, trusts on your local instance that is beyond the proxy, and might not use SSL. Which can be misleading/wrong.
Which is probaly the case on a EC2 instance... as the SSL termination is beyond load balancers/HA Proxies.
i usually add the following to my AppServiceProvider.php
public function boot()
{
if (Str::startsWith(config('app.url'), 'https')) {
\URL::forceScheme('https');
} else {
\URL::forceScheme('http');
}
}
Of course this needs to ensure you've set app.url / APP_URL, if you are not using that, you can just get rid of the if statement. But is a little less elegant, and disallows you to develop on non https

Problem with Laravel routes - all sub folder traffic ending up in route view

Sorry - was difficult to give this one a clear title! But I have an issue and a difference between how my local laravel install is dealing with some routes compared to my live server.
Locally, I have this working:
Route::get('/blog', 'BlogController#home');
Route::get('/blog/{post_slug}', 'BlogController#viewPost');
As you can probably guess, I want to serve up a list of posts via the home() function if /blog is hit. Then all other traffic with a "slug" after /blog/, I want to load the blog post.
This all works locally.
However on live,
/blog/my-blog-post
Is serving up the home() function every time.
Where would I start with debugging this. Laravel versions? Server caching?
Maybe you can do this in laravel 5.7+
Route::prefix('blog')->group(function () {
Route::get('/', 'BlogController#home');
Route::get('/{post_slug}', 'BlogController#viewPost');
});
before just use: php artisan optimize, to clear all cache route and config.
for more info see the docs

How to properly connect Nuxt.js with a laravel backend?

I am starting a new project, Nuxt.js for the frontend and Laravel for the backend.
How can I connect the two?
I have installed a new Nuxt project using create-nuxt-app, and a new laravel project.
As far as I have searched, I figured I need some kind of environment variables.
In my nuxt project, I have added the dotenv package and placed a new .env file in the root of the nuxt project.
And added CORS to my laravel project, as I have been getting an error.
The variables inside are indeed accessible from the project, and im using them
like this:
APP_NAME=TestProjectName
API_URL=http://127.0.0.1:8000
And accessing it like this:
process.env.APP_NAME etc'
To make HTTP calls, I am using the official Axios module of nuxt.js, and to test it i used it in one of the components that came by default.
The backend:
Route::get('/', function () {
return "Hello from Laravel API";
});
and from inside the component:
console.log(process.env.API_URL)//Gives 127.0.0.1:8000
//But this gives undefined
this.$axios.$get(process.env.API_URL).then((response) => {
console.log(response);
});
}
What am I doing wrong here?
I have tried to describe my setup and problem as best as I can. If I overlooked something, please tell me and I will update my question. Thanks.
Taking for granted that visiting https://127.0.0.1:8000/ in your browser you get the expected response, lets see what might be wrong in the front end:
First you should make sure that axios module is initialized correctly. Your nuxt.config.js file should include the following
//inclusion of module
modules: [
'#nuxtjs/axios',
<other modules>,
],
//configuration of module
axios: {
baseURL: process.env.API_URL,
},
Keep in mind that depending on the component's lifecycle, your axios request may be occurring in the client side (after server side rendering), where the address 127.0.0.1 might be invalid. I would suggest that you avoid using 127.0.0.1 or localhost when defining api_uris, and prefer using your local network ip for local testing.
After configuring the axios module as above, you can make requests in your components using just relative api uris:
this.$axios.$get('/').then(response => {
console.log(response)
}).catch(err => {
console.error(err)
})
While testing if this works it is very helpful to open your browser's dev tools > network tab and check the state of the request. If you still don't get the response, the odds are that you'll have more info either from the catch section, or the request status from the dev tools.
Keep us updated!
Nuxt has a routing file stucture to make it easy to set up server side rendering but also to help with maintainability too. This can cause Laravel and Nuxt to fight over the routing, you will need to configure this to get it working correctly.
I'd suggest you use Laravel-Nuxt as a lot of these small problems are solved for you.
https://github.com/cretueusebiu/laravel-nuxt

Laravel 5 Route Strange Behaviour

I have a Laravel site set up on a Homestead box, so I'm accessing it on sitename.app:8000. I have a route called "news" but when I try to go to sitename.app:8000/news I get oddly bounced out to sitename.app/news/.
If I change the routename to "news2" I can access the desired controller action as per normal at sitename.app:8000/news2. So somehow it's "news" itself that has become uncooperative - and I'm pretty sure that we aren't even getting as far as the NewsController, when I try to access that url.
Can anyone work out from these symptoms what might be going wrong? One "news"-related change I made at some point was to add $router->model('news', "App\News"); in the boot method of the RouteServiceProvider, but removing this doesn't seem to make the difference.
ETA: People keep asking for the routes.php file. I can literally remove everything from the file except
Route::get('news', function() {
return "hello world";
});
Route::get('news2', function() {
return "hello world";
});
and /news2 will work but /news will bounce me out. So I remain pretty convinced that the problem is somewhere deeper than routes.php...
I finally worked out what boneheaded action of mine had been causing this behaviour!
I had created a folder in /public named "news"... i.e. with the same name as an important route. Not sure exactly what havoc this was wreaking behind the scenes for Laravel every time a request for /news was being made, but one can assume it was nothing good.
Advice for anyone tearing their hair out over a route that "mysteriously doesn't work" - check your public folder for possible collisions!
This is a known issue Larvel missing port
The easiest way to solve this problem is to go to public/index.php and set the SERVER_PORT value.
$_SERVER['SERVER_PORT'] = 8000;
Don't forget to set the base url in the config if you are using links on website, the url generator uses the base-url defined in the config.
Last option is to change the vm portfoward in VagrantFile to point to port 80 and use port 80 for your app.

Resources