开始使用 Silex

Getting started with Silex

我正在掌握 Silex Routing,这是一个显示各种页面的简单应用程序。

我可以注册 returns "Hello World" ->get('/') 路由,但是当我 运行 ->get('/hello/{ name}') 我 运行 进入一个 Object not found 404 Error.

多个教程和建议坚持我这样做但是我不需要先注册路线吗?

<?php

/* include the silex autoload */
require_once __DIR__.'/vendor/autoload.php';

$app = new Silex\Application();

$app['debug'] = true;

/* silex uses anonymous functions to define routes */
$app->get('/', function() use($app) {

return 'Hello World!';

});

$app->get('/hello/{name}', function($name) use($app) {

return 'Hello '.$app->escape($name);
});

$app->run();

我也试过了

$app->get("/hello/{name}", function($name){
  return 'Hello '.$name;
});

非常感谢任何帮助

正如我在评论中所说,Silex 通常不带 .htaccess 文件,因此所有请求都必须在 URL 中包含 index.php 才能使路由正常工作。

当然这并不意味着您不能自己创建.htaccess 文件。事实上,在 Silex 文档中有一章是关于服务器配置的,提供了一个简单的 .htaccess 配置。 Link 到章节 - Webserver Configuration.

我会在此处复制代码以备将来请求:

<IfModule mod_rewrite.c>
    Options -MultiViews

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

关于 RewriteBase 的注释:

If your site is not at the webroot level you will have to uncomment the RewriteBase statement and adjust the path to point to your directory, relative from the webroot.