Perl-Evozon / PearlBee

116 stars 44 forks source link

What is the utility of `app_url`? #51

Open book opened 9 years ago

book commented 9 years ago

In many places in the application, we see session('app_url') (in the code) or [% session.app_url %] (in the templates). I don't understand what this provides, since all absolute links (starting with /) would resolve to links in the application.

The only thing really needed would be the mount point, if the Plack application was mounted under some path below the absolute root (/), so that absolute links would include it.

book commented 9 years ago

I see it was added in c3ba157715dffab7688fac4ee99da1744179ebf1, which suggests it was needed for communicating the full application URL outside of HTTP (e.g. email). Maybe a better place for that information would be in the settings table.

book commented 9 years ago

7cb81d258103cee266135936a32952578f90f86a suggests that it was also meant to include the mount point in URL shown within the application.

The Plack::App::URL documentation explains how it works, and that the proper Plack environment variable for that are SCRIPT_NAME and PATH_INFO.

book commented 9 years ago

Instead of app_url, I suggest to create a new column host in the settings table, to contain the web site hostname (e.g. www.pearlbee.org), and use SCRIPT_NAME and PATH_INFO to construct the path of the URL.

A quick example of how that works:

use Plack::Builder;

my $app = sub {
    my ($env) = @_;
        [ 200, [], [ join "\n",
            "SCRIPT_NAME = $env->{SCRIPT_NAME}",
            "PATH_INFO = $env->{PATH_INFO}",
            "SCRIPT_NAME.PATH_INFO = $env->{SCRIPT_NAME}$env->{PATH_INFO}",
        ]
    ];
};

builder {
    mount "/"    => $app;
    mount "/bar" => $app;
};

And here are the results:

Just preprend the value of your host setting to SCRIPT_NAME and PATH_INFO and you have your full URL.