1

我正在尝试在子目录中的 Apache(共享主机)上运行一个小型 Mojolicious Lite 应用程序:http://www.example.com/mymojoapp/. 我主要尝试遵循本指南

是服务器上的mymojoapp一个实际目录,app结构如下:

mymojoapp
|- .htaccess
|- app.pl
|- public
|  +- images
|  |  +- ... (image files) ...
|  +- css
|  |  +- ... (css files) ...
|  +- js
|     +- ... (js files) ...
+- templates
   |- layouts
   |  |- index-layout.html.ep
   |  +- other-page-layout.html.ep
   |- index.html.ep
   +- other-page.html.ep

.htaccess内容:

AddHandler cgi-script .pl
Options +ExecCGI

IndexIgnore *

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ app.pl/$1 [L]

app.pl内容(perlmlperl+ 用户安装模块的路径,包括 Mojolicious):

#!/usr/bin/perlml

use Mojolicious::Lite;

hook before_dispatch => sub {
    my $c = shift;
    $c->req->url->base(Mojo::URL->new(q{http://www.example.com/mymojoapp/}));
};

get '/' => 'index';

get '/u/:username' => sub {
    my $self = shift;
    $self->stash(profile => $self->stash('username'));
} => 'user-profile-page';

app->secrets(['app','secrets']);
app->start;

在两者中index-layout.html.epother-page-layout.html.ep我都将 css(和图像)称为<link rel="stylesheet" href="<%= url_for %>/css/styles.css">. 当我访问基本 URL ( http://www.example.com/mymojoapp/) 时,页面以正确的样式显示,因为它url_for为我 /mymojoapp/提供了 stylesheet = 的路径/mymojoapp/css/normalize.css。但是当我访问时http://www.example.com/mymojoapp/u/someuser,CSS路径是/mymojoapp/u/someuser/css/normalize.css并且样式没有应用。

我觉得我错过了一些重写规则,或者它可能可以在before_dispatch钩子中修复,但到目前为止我无法弄清楚。

如何确保在我的应用中生成的所有页面都获得正确的基本 URL?

4

1 回答 1

1

从@jabberwok 在IRC 的#mojo 上收到了答案:

而不是<%= url_for %>/css/styles.css我需要使用<%= url_for('/css/styles.css') %>.

于 2017-07-23T23:37:52.747 回答