laravel-enso / enso

Laravel Vue SPA, Bulma themed. For demo login use `admin@laravel-enso.com` & `password` -
https://www.laravel-enso.com
MIT License
1.08k stars 277 forks source link

Possible to subdomain route with route "/" #302

Closed stotes closed 4 years ago

stotes commented 4 years ago

This is a bug maybe?

Prerequisites

Description

Preface first by saying that I'm not sure if this is a bug or just simply me failing to correctly implement. I'm attempting to add a component to an Enso app that resides on the root of a subdomain route with its own middleware group. If I provide the route anything but the base path eg "/enrol" it works however I can not seem to figure out to work with just the root path "/".

Route::group(['domain' => "enrol.test"], function () {
    Route::middleware('enrol')->namespace('App\Http\Controllers')->group(function () {
        Route::get('/', 'EnrolController@test')->name('test');  // does not work 
        Route::get('/test', 'EnrolController@test')->name('test1'); //works
    });
 });

I've tried modifying the web.php routes to:

Route::group(['domain' => 'test'], function () {
    Route::view('/{any}', 'index')
    ->where('any', '.*');
});

Steps to Reproduce

Setup a route using domain group, attempt to use root path for it's url.

Expected behavior

Can use root path in subdomains to direct to other parts of my app not using Enso middleware.

Actual behavior

Always redirects to Enso middleware /login

aocneanu commented 4 years ago

try something like

Route::domain('enrol.'.Config::get('app.url'))
    ->group(...);

Put that above enso's

Route::view('/{any}', 'index')->where('any', '.*');
stotes commented 4 years ago

Unfortunately that doesn't work for me.

No matter what I try it will redirect to /login for any root path of subdomains.

aocneanu commented 4 years ago

Could you provide your web.php content here?

stotes commented 4 years ago

The main part of my application runs in a package that sits ontop of Enso.

Steps I've taken. Attempted to set the domain value of the main app to the tld.

//web.php in app [/routes/web.api] with setting Domain for base application
<?php
$hostname = preg_replace('#^https?://#', '', Config::get('app.url'));
Route::domain($hostname, function () {
    Route::view('/{any}', 'index')
    ->where('any', '.*');
});
//routes in package [/vendor/adamsto/presto/src/routes/enrol.php]
$hostname = preg_replace('#^https?://#', '', Config::get('app.url'));
Route::group(['domain' => "enrol.$hostname"], function () {
    Route::middleware([])->namespace('Presto\App\Http\Controllers')->group(function () {
        Route::get('test', 'EnrolController@test')->name('test');
    });
});
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
| Domain       | Method        | URI                                                         | Name                                         | Action                                                                                 | Middleware                                       |
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
| enrol.presto | GET|HEAD      | /                                                           | test                                         | Presto\App\Http\Controllers\EnrolController@test                                       |                                                  |
| presto       | GET|HEAD      | {any}                                                       |                                              | Illuminate\Routing\ViewController                                                      | web                                              |
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+

Attempted to as you suggested using the Route::domain, without setting main app to domain. Result: Redirects to /login.

<?php
$hostname = preg_replace('#^https?://#', '', Config::get('app.url'));
Route::domain("enrol.$hostname")->namespace('\Presto\App\Http\Controllers')->group(function () {
        Route::get('', 'EnrolController@test')->name('test');
});
Route::view('/{any}', 'index')
    ->where('any', '.*');
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
| Domain       | Method        | URI                                                         | Name                                         | Action                                                                                 | Middleware                                       |
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
| enrol.presto | GET|HEAD      | /                                                           | test                                         | Presto\App\Http\Controllers\EnrolController@test                                       |                                                  |
|              | GET|HEAD      | {any}                                                       |                                              | Illuminate\Routing\ViewController                                                      | web                                              |
+--------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+

I've also attempted to setup another route file in the main app (rather than in the package) that registers the enrol.php routes before the web.api in the RouteServiceProvider.php

public function map()
    {
        $this->mapEnrolRoutes();

        $this->mapApiRoutes();

        $this->mapWebRoutes();
    }

    protected function mapEnrolRoutes()
    {
        Route::middleware([])->group(base_path('routes/enrol.php'));
    }

Everything I've tried seems to redirect any subdomain root url to the /login page.

aocneanu commented 4 years ago

I finally had time to play with this.

Please note that this is more of a Laravel topic ;)

In an Enso project I did this:

// routes/web.php

Route::domain('app.'.Config::get('app.domain_url'))
    ->group(fn () => Route::view('/{any}', 'index')->where('any', '.*'));

Route::view('/', 'hello');
// config/app.php

'domain_url' => (new Collection(
    explode('.', preg_replace('(^https?://)', '', env('APP_URL')))
))->take(-2)->implode('.'),

now if I go to project.test I'm routed to the hello view, but if I go to app.project.test I'm routed to the Enso project (login etc)

stotes commented 4 years ago

Thank you very much for the reply. I really really appreciate your help. If you drink I'd like to buy you a beer! Haha. I'm so sorry if this is non Enso related. I just for the life of me have been unable to get this to work.

I've tried what you suggested but haven't been able to get it to work.

#Hosts File
127.0.0.1   presto
127.0.0.1   enrol.presto
127.0.0.1   app.presto
//web.php route file
Route::domain('app.'.Config::get('app.domain_url'))->group(fn () => Route::view('/{any}', 'index')->where('any', '.*'));

//enrol.php route file
Route::view('/', 'hello');

//RouteServiceProvider.php
    public function map()
    {
        $this->mapEnrolRoutes();

        $this->mapApiRoutes();

        $this->mapWebRoutes();
    }

    protected function mapEnrolRoutes()
    {
        Route::middleware([])->group(base_path('routes/enrol.php'));
    }

//app.php config
'domain_url' => collect(
        explode('.', preg_replace('(^https?://)', '', env('APP_URL')))
    )->take(-2)->implode('.'),

hello.blade.php


<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="icon" type="image/png" href="/images/favicon.png">
    <title>Hello</title>
<body>
Hello
</html>
ROUTES
+------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
| Domain     | Method        | URI                                                         | Name                                         | Action                                                                                 | Middleware                                       |
+------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+
|            | GET|HEAD      | /                                                           |                                              | Illuminate\Routing\ViewController                                                      |                                                  |
| app.presto | GET|HEAD      | {any}                                                       |                                              | Illuminate\Routing\ViewController                                                      | web                                              |
+------------+---------------+-------------------------------------------------------------+----------------------------------------------+----------------------------------------------------------------------------------------+--------------------------------------------------+

I've uploaded some captures to share the process. FireFox Private Window demonstration Code, HOSTS,Nginx,Routes

stotes commented 4 years ago

I believe the issue is related to my development machine's Windows Subsystem Linux configuration. I'll mark this as resolved and provide a solution if I come to one.

Thanks for your help @aocneanu

aocneanu commented 4 years ago

Regarding the beer, if you want, you can make a donation