2017-01-22 81 views
2

这段代码来自heroku的一个例子。但除了路线/,我添加的任何东西都不起作用。它显示404:Heroku silex route show 404 except“/”

在此服务器上找不到请求的URL/e。

$app->match('/', function(Symfony\Component\HttpFoundation\Request $request) use ($app) { 
    return $app['twig']->render('index.twig'); 
}); 

$app->match("/dump", function(Symfony\Component\HttpFoundation\Request $request) use ($app) { 
    return new Response('Thank you for your feedback!', 201); 
})->bind("dump"); 
$app->get("/t", function() use ($app) { 
    return new Response('Thank you for your feedback!', 201); 
})->bind("t"); 
$app->get("/d", function() { 
    return new Response('Thank you for your feedback!', 201); 
})->bind("d"); 
$app->get("/e", function() { 
    return new Response('Thank you for your feedback!', 201); 
}); 
$app->run(); 

编辑1 我部署它直接Heroku的服务器上(Heroku的自动生成每个推到主分支)

编辑2 我的工作区:

bin\worker.php 
www\index.php <== the snippet is from this file 
www\list.php 
apache_app.conf 
app.php <== basic init, return Silex\Application $app 
Procfile 

apache_app.conf的内容复制自this link。 RewriteEngine叙述在

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule .? - [L] 

RewriteRule .? %{ENV:BASE}/app.php [L] 

我想,我需要以某种方式修改Apache的配置,但我不明白htaccess的语法。

+0

是'/ index.php/e'的作品吗?你有没有配置你的网络服务器?你使用的是Apache还是nginx? – Federkun

+0

@Federkun不,它没有用。由于我将它部署在heroku服务器上,我不认为我可以配置任何东西。据我所知,他们使用apache2。 – Ngoc

+0

请参阅http://silex.sensiolabs.org/doc/2.0/web_servers.html#apache。 '/ index.php/e'(其中'/ index.php是你的前端控制器)应该可以工作。当你转到'/ index.php'时,它是否正确返回索引页? – Federkun

回答

0

apache2的,你可以在 “/网络” 添加.htaccess文件

<IfModule mod_rewrite.c> 
    Options -MultiViews 

    RewriteEngine On 
    #RewriteBase /path/to/app 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [QSA,L] 
</IfModule> 

并设置Procfile为:

web: vendor/bin/heroku-php-apache2 web/ 

对于nginx的,创建内容的nginx的conf文件:

location/{ 
    # try to serve file directly, fallback to rewrite 
    try_files $uri @rewriteapp; 
} 

location @rewriteapp { 
    # rewrite all to index.php 
    rewrite ^(.*)$ /index.php/$1 last; 
} 

location ~ ^/(index|index_dev)\.php(/|$) { 
    try_files @heroku-fcgi @heroku-fcgi; 
    internal; 
} 

,并设置Procfile为:

web: vendor/bin/heroku-php-nginx -C server_conf/nginx.conf web/