Laravel subdomain routing not working, results in 404 - laravel

I am moving from local development with docker-compose to Gitpod.io, an remote dev environment. There I am using the exact same docker images and the same configuration. I am also using local domains (e.g. domain.test) on specific ports in order to address the different applications running on Gitpod. I've established an SSH tunnel, so localhost:port is mapped to Gitpod.
But now I have a strange behavior using subdomains on Laravel.
Opening http://foo.domain.test:8008/foobarius with the following code, results in a 404:
Route::name('foo')
->domain('foo.domain.test:8008')
->group(function() {
Route::get('foobarius', function() {
return "fooo";
});
});
Doing the same but removing the domain part works when opening http://foo.domain.test:8008/foobarius
Route::name('foo')
->group(function() {
Route::get('foobarius', function() {
return "fooo";
});
});
Having the endpoint registered and calling php artisan route:list gives me the following output:
Somehow Laravel seems to ignore the subdomain or does not find the right route within all registered routes.
My hosts file:
127.0.0.1 domain.test
127.0.0.1 foo.domain.test
There is no other docker application running.

Related

Stancl\Tenancy\Exceptions\NotASubdomainException Hostname DOMAIN.COM does not include a subdomain

I am using Laravel Forge with AWS EC2 Instance to build a CI/CD Pipeline for a SAAS Laravel project.
Error Attachment: https://i.stack.imgur.com/hsMdK.png
The configurations are fine but when it comes to a tenancy-based project I get this error.
(Stancl\Tenancy\Exceptions\NotASubdomainException
Hostname domain.com does not include a subdomain)
Note: I am using TenancyForLaravel package
I got it fixed in 2 steps.
It wasn't a server-side issue.
1st Step
The TenancyForLaravel package was making an impact, I need to add initializing code for sub-domains in my laravel project.
app/Providers/TenancyServiceProvider.php
public function boot() {
\Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain::$onFail =
function () {
return redirect(env('CENTRAL_DOMAIN'));
};
}
2nd Step
In your laravel root folder
/config/tenancy.php
add your domain name in central_domain
'central_domains' => [
'127.0.0.1',
'localhost',
'domain.com',
],

404 - NOT FOUND in laravel 8

I'm new to laravel and I've just installed the package on a local server.
I used php artisan serve to open the app
I made a simple route to test:
Route::get('a', function () {
return 'hi there';
});
but when I try to open this route I got 404 not found.

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

Laravel route works with internal webserver but not with WAMP

I defined the following route in /app/Http/routes.php:
Route::get('products', function () {
return App\Product::all();
});
My Laravel Installation sits in C:\wamp\www\product-service\
When running WAMP, Laravels Welcome Page will be displayed (URL: http://localhost/product-service/public/)
However, the URL http://localhost/product-service/public/products won't work, Error Message "Not Found. The requested URL /product-service/public/product was not found on this server".
Now, when I start the internal test server
php artisan serve --host=127.0.0.1
The URL http://localhost:8000/products will work fine.
Why is that?
First of all, check if you've the proper .htaccess in your public folder. If the file is present, maybe your WAMP environment has the mod_rewrite module disabled. Check the apache conf file in:
{wamp_dir}/apache/conf/httpd.conf
check for this line:
#LoadModule rewrite_module modules/mod_rewrite.so
remove the # at the beginning. Check then for the AllowedOverride param and change No to All. Restart your apache server and give it a try. Last but no least, check the Laravel docs here for another htaccess file that can be useful to you

Laravel subdomain config

Our staging environment on a project uses the same domain AKA staging.mydomain.com. How do I get the Laravel database config to point to a different DB as the config switch is based on the hostname which is the same for staging. and www.?
You can update the detectEnvironment method to use a closure function and run your login in there to determine if your application is local or not.
update bootstrap/start.php like this:
$env = $app->detectEnvironment(function() {
return preg_match('/staging/', $_SERVER['HTTP_HOST']) ? 'staging' : 'production';
});
Now when you are visiting your laravel project from http://staging.xxx URL, it will detect it as staging environment.
Now, you can place the database config specific to staging env in here:
app/config/staging/database.php
This should do the trick.

Resources