Silex入门

时间:2015-07-15 16:01:14

标签: php silex

我正在处理Silex Routing,一个显示各种页面的简单应用程序。

我可以注册 - > get('/')路由返回“Hello World”,但是当我运行时 - > gt; get('/ hello / {name}')我遇到了一个对象找不到404错误。

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

<?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;
});

任何帮助非常感谢

1 个答案:

答案 0 :(得分:3)

正如我在评论中所说,public static class EnumerableExtensions { public static ForEach<T> ForEach<T>(this IEnumerable<T> enumerable, Action<T> body) { return new ForEach<T>(enumerable, body); } } public class ForEach<T> { private IEnumerable<T> enumerable; private Action<T> forEach; private Action<Exception> onError; private Action ifEmpty; public ForEach(IEnumerable<T> enumerable, Action<T> forEach) { this.enumerable = enumerable; this.forEach = forEach; } public ForEach<T> OnError(Action<Exception> onError) { this.onError = onError; return this; } public ForEach<T> IfEmpty(Action ifEmpty) { this.ifEmpty = ifEmpty; return this; } public void Execute() { try { var isEmpty = true; foreach (var obj in enumerable) { isEmpty = false; forEach(obj); } if (isEmpty && ifEmpty != null) ifEmpty(); } catch (Exception ex) { if (onError != null) onError(ex); else throw ex; } } } 通常没有.htaccess文件,因此所有请求都必须在URL中包含Silex才能使路由正常工作。

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

我将此处的代码复制到以后的请求中:

index.php

关于<IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On #RewriteBase /path/to/app RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] </IfModule> 的说明:

  

如果您的站点不在webroot级别,则必须取消注释RewriteBase语句并调整指向您的目录的路径,相对于webroot。

相关问题