Silex路由问题 - 删除页面名称和目录名称

时间:2015-11-08 09:33:22

标签: php .htaccess symfony symfony1 silex

首先,我是Silex PHP Framework的新用户,我正在尝试为我的Android应用创建一个RESTApi。

我的目录结构

  

ABC
  ----厂商
  ----网络
  --------的index.php
  --------的.htaccess
  ---- composer.json
  ---- composer.lock

我的Index.php文件编码

<?php

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/hello/{id}', function ($id) use($app) {
    return 'Hello '.$app->escape($id); 
});

$app->get('/', function () {
    return 'Hello!';
});

$app->run();

我的.htaccess文件编码

RedirectMatch permanent ^/index\.php/(.*) /$1
<IfModule mod_rewrite.c>
    Options -MultiViews    
    RewriteEngine On
    RewriteBase /abc/web/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?!web/).*)$ web/$1 [NC,L]
</IfModule>

当我点击此网址http://127.0.0.1/abc/web/

它的工作非常好并得到回复Hello!

但是当我点击此网址http://127.0.0.1/abc/web/hello/123

所以我得到了像这样的错误

Not Found

The requested URL /abc/web/hello/123 was not found on this server.

如果我点击此网址http://127.0.0.1/abc/web/index.php/hello/123,那么它的工作正常,我的回复就行了。 Hello 123

因此,MY QUESTION是我从网址中删除网页名称index.phpdir名称web的方法,我希望我的网址看起来像http://127.0.0.1/abc/hello/123

这可能吗?怎么样?

请帮助,提前致谢。

1 个答案:

答案 0 :(得分:1)

所发生的事情是,您的重写规则不是通过Silex前端控制器(index.php)发送请求,这是获得友好路由工作所必需的。我强烈建议您阅读routing documentation以及example Apache configuration

要对此进行排序,以下内容可能适用于.htaccess:

    RewriteRule ^((?!web/).*)$ web/index.php/$1 [NC,L]

我不完全确定^((?!web/).*)$正则表达式的目的是什么,理想情况是:

    RewriteRule ^ index.php [QSA,L]

根据文档。尽管如此,你的要求可能要求你摆弄它,直到它起作用。