Mixed content issue- Content must be served as HTTPS || Lumen || Swagger API - laravel

Mixed Content: The page at 'https://api.xyz.com/api/documentation' was loaded over HTTPS,
but requested an insecure stylesheet 'http://api.xyz.com/swagger-ui-assets/swagger-ui.css?v=26ec363936a21921c9fec290e551e3eb'.
This request has been blocked; the content must be served over HTTPS.
I know how to solve this problem in Laravel but unable to solve this in case of Lumen.
Tried Solutions:
Added below line in AppServiceProvider
URL::forceSchema('https');

update boot() method in app/Providers/AppServiceProvider.php file as bellow
public function boot()
{
if(env('APP_ENV') !== 'local') {
URL::forceScheme('https');
}
}
alse update APP_ENV to APP_ENV=prod in .evn file

Check the APP_URL in the .env file which is set to http://localhost
Heroku uses https://appname.herokuapp.com since it's in production but lumen uses http://localhost for dev mode so I fixed it by running heroku config:set APP_URL=https://localhost

Related

Unable to boot ApiServiceProvider, configure an API domain or prefix in Laravel

I am using Laravel-8 as restful api. Also I am using Dingo Package. Initially the url was:
http://localhost:8888/myapp/server/public
And it was loading correctly.
However, I decided to remove public from the url. So I followed these steps:
Rename server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root folder.
When I tried to reload the url as:
http://localhost:8888/myapp/server
I got this error:
Unable to boot ApiServiceProvider, configure an API domain or prefix
C:\xampp\htdocs\myapp\server\vendor\dingo\api\src\Provider\DingoServiceProvider.php
protected function registerConfig()
{
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/api.php'), 'api');
if (! $this->app->runningInConsole() && empty($this->config('prefix')) && empty($this->config('domain'))) {
throw new RuntimeException('Unable to boot ApiServiceProvider, configure an API domain or prefix.');
}
}
Even with:
http://localhost:8888/mygeapp/server/public/
The same error still applies
How do I resolve this?
Thanks
Please be sure to configure your IPI PREFIX in the env file
API_PREFIX=api

Laravel - How to clear config:cache on each request

I am working on a project which will have 50 subdomains and I found a solution to load separate .env file based on a domain name and everything is working fine... now my problem is
I have to run command config:cache to clear the cache so sytem can load relevant .env file, otherwise, it keeps loading the older .env file. How can i ask system to do cache clear on each load in bootstrap/app.php file???
My Code to load .env files in bootstrap/app.php
$domain = '';
if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
$domain = $_SERVER['HTTP_HOST'];
}
if ($domain) {
$dotenv = \Dotenv\Dotenv::create(base_path(), '.env.'.$domain.'.env');
try {
$dotenv->overload();
} catch (\Dotenv\Exception\InvalidPathException $e) {
// No custom .env file found for this domain
}
}
I would advise against doing that because things WILL break.
However, to answer your question, you can use Artisan::call('config:clear') inside a middleware that you can call on each request.
But instead of doing that, you could build a middleware that detects the subdomain you're getting the request from and then call the command instead, just to avoid that extra load.
I used another way to solve this on my project. It is setting the config dynamically according to the request. The config is only valid for the current request. If the count of the dynamic config is less you can use
Config::set('myConfig.hostName', $hostName);
Before doing that you must use the package
use Illuminate\Support\Facades\Config;

Laravel 5 url() doesn't contain scheme

I'm quite new in Laravel.
I'm working on an existing Laravel 5.8 project. I installed it locally with HomeStead.
I noticed a strange behaviour on redirects: considering my homepage is http://project.test/, when there is a redirection, say to /redirected, Laravel sets the location to http://project.test/://project.test/redirected! As I tried to figure out wat was going on, I saw that the url('/') Laravel function gives me ://project.test instead of http://project.test.
request()->getSchemeAndHttpHost() gives http://project.test.
Example in web.php:
Route::get('/info', function (Request $request) {
echo url('/'); // gives '://project.test'
echo '<br>';
echo request()->getSchemeAndHttpHost(); // gives 'http://project.test'
// This will lead me to 'http://project.test/://project.test/redirect'
// return redirect('/redirect');
});
I have the same problem with the static resources.
Config:
'url' => env('APP_URL', 'http://project.test') in config/app.php
APP_URL=http://project.test in the .env file.
I certainly missed something, but so far I couldn't find any information about this.
when you redirect check if you wrote : "Redirect::home()" , try "return Redirect::home()" (note the return)
I tested your routes, and it's working nicelly for me.
I give you my config,just replace "tlara" wich is my project name with your's : "project"
in host.sam
127.0.0.1 tlara.test #laragon magic! (note is not my ip 192.xx.xx, 127.0.0.1 is universal localhost)
in .env
APP_URL=http://localhost
..
REDIS_HOST=127.0.0.1 (i don't know if it is revelant)
..
SESSION_DOMAIN='localhost'
clear
clear caches (routes, caches, view) (may be useless)
test
tlara.test/info
127.0.0.1/info
localhost/tlara/public/info

Lumen dot in URL

I am serving a file via Lumen route and URL contains dot and extension file. and when URL contains dot Lumen look for resources not executing routes.
I've tried to run it on production server and it works but on development environment it does not work.
This is my route:
$router->get('file/{file}', function (Request $request, $file) {
echo $file;
});
When I try to open this URL route will be run:
http://localhost:9001/file/photo.jpg
When I try to open this URL Lumen look for resources:
http://localhost:9001/file/photojpg

Unable to get laravel/database and restler working with versioning

I have an existing website up and running, and now I want to add a REST interface to it in an api subdirectory. I'm not able to get this to work with versioning. I installed like so (no errors):
$ php ~/bin/composer.phar create-project laravel/database --prefer-dist api
$ cd api
$ php ~/bin/composer.phar require restler/framework 3.0.0-RC6
Then I uncommented the lines in public/index.php related to Restler and add a new API class that just echos a string. If I run this via php artisan serve and look at it through the localhost URL, then the method works.
Now I want to enable versioning, so I added these lines to public/index.php
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
And in app/controllers I created a v1 directory and moved Test.php into that. I also added a namespace directive to the file of the format namespace A\B\v1
When I restart the artisan server and query the API, I get a 404 error. I've tried as both http://localhost:8000/Test and http://localhost:8000/v1/Test
What have I forgotten to do?
Here is how I made it to work. Note the folder where I placed the api class file.
in index.php
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
$r = new Restler();
$r->addAPIClass('A\B\Test');
Test.php kept in app/controllers/A/B/v1/Test.php
<?php namespace A\B\v1;
class Test
{
public function get()
{
return 'working';
}
}
Both http://localhost:8000/v1/test and http://localhost:8000/test return "working"

Resources