2010-05-26

使用symfony filter 过滤URL

类归于: symfony — 标签:, , szn000 @ 09:30

这是一个过滤URL中 多个’/'的例子
修改 apps/myapp/config/filters.yml
添加代码
myFilter:
class: myFilter

(myFilter可自定义)
创建文件myFilter.class.php
创建位置apps/myapp/lib/
文件内容如下
使用preg_match 是因为 ereg 过时
注意preg_match 匹配正则时以’/'开始和结束
class myFilter extends sfFilter
{
public function execute ($filterChain)
{
//提取uri
$str = $_SERVER['REQUEST_URI'];
//判断是否匹配正则
if(preg_match("/(.*\/{2,}.*)((\.html)*)/",$str)){
//要跳转的页面
header("Location:/error.html");
//跳转后代码不执行
exit();
}
//执行下一个filter
$filterChain->execute();
}
}

修改apps/myapp/config/routing.yml
添加代码 此代码用来定义URL /error.html 要执行的模块和动作
Error:
url: /error.html
param: { module: index, action: error404}

2010-05-17

使用symfony routing.yml 修改URL

类归于: symfony — 标签:, szn000 @ 12:04

由于不规范的URL带有程序和数据库的结构信息会给程序带来隐患并且在程序中经常需要使用?name=value来传递变量的值所以修改URL非常重要.
在symfony中 使用 routing.yml 文件就可以修改URL
下面是一个URL修改的例子
将 project/news/show?title=123 修改成 project/news/123.html
在projiect/apps/myapp/config/routing.yml 添加下列代码
shownews:
url: /news/show
param: { module: news, action: show }

上面代码是定义一个 @shownews 路由使其访问 news/show
注意:每次修改routing.yml 后需要执行 symfony cc 清理缓存
在页面中添加如下代码
<?php echo link_to($name, '@shownews?title='. $title);?>
$name是超链接名称
$title是要传递的值
(如果 $name=’测试’ $value=’123′)
上面的php代码转换成HTML代码如下
<a href="/news/show?title=123">测试</a>
在projiect/apps/myapp/config/routing.yml 修改代码
shownews:
url: /news/:title.html
param: { module: news, action: show }
requirements:
title: \w+

PHP代码

将转换成
<a href="/news/123.html">测试</a>

2009-05-25

怎么在app中构造其它app的链接(二)

类归于: symfony — 标签:zhuozi @ 17:15

写一个class

// apps/backend/config/backendConfiguration.class.php
class backendConfiguration extends sfApplicationConfiguration
{
protected $frontendRouting = null;
public function generateFrontendUrl($name, $parameters = array())
{
return 'http://frontend.example.com'.$this->getFrontendRouting()
->generate($name, $parameters);
}
public function getFrontendRouting()
{
if (!$this->frontendRouting)
{
$this->frontendRouting = new sfPatternRouting(new sfEventDispatcher());
$config = new sfRoutingConfigHandler();
$routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').
'/frontend/config/routing.yml'));
$this->frontendRouting->setRoutes($routes);
}
return $this->frontendRouting;
}
}

在action中可以用下面的动作,就可以跳到其它的app中的动作,下面的‘hello’,是frontend/config配置文件routing中的一条路由规则

$this->redirect($this->getContext()->getConfiguration()->
generateFrontendUrl('hello', array('name' => 'Bar')));


function link_to_frontend($name, $parameters)
{
return sfProjectConfiguration::getActive()->
generateFrontendUrl($name, $parameters);
}

2009-05-23

怎么在app中构造其它app的链接(一)

类归于: CentOS/Redhat, symfony — 标签:zhuozi @ 16:44

最近开发的一个项目要求我们要在一个app中构造另一个app中的链接并使用。

我们现在有一个管理平台,app名字   admin, 我们还有一个PC展示平台 app名字 pc

我们知道使用symfony中链接辅助函数  url_for()  可以生成我们想要的URL,可是,默认的情况下,这种生成出来的URL是显示前端控制器名字的(控制器名字根据所在开发模式的不同,显示的也不一样)。

比如: 我们在admin这个平台使用  url_for(’member/create’)    生成的url是   admin_dev.php/member/create, 很显然这种URL不是我们所期望的,我们不希望还要去解析判断。

在apps/myapps/settings.yml 中有一项设置  no_script_name: off  这项是控制时候在URL中显示前端控制器的名字。

当我们把它开启的时候,再使用url_for(’member/create’)的时候,就会生成  member/create, 这样就会得到没有前端控制器名字的URL。

其实,最理想的状态是,我在使用某一个链接辅助函数的时候,直接就可以获得某一个平台的URL,比如在admin的平台返回给我一个  http://www.foolbirds.com/pc_dev.php/article/show  这样的链接。 可是找了一下下午也没有找到一个,或许需要自己写一个构造这样的URL来满足项目的需求了。

WordPress 所驱动