转载请附上本文链接,谢谢。
最近给外包的项目写前端路由,被基友提醒了【你的路由只负责重定向,不给模板传递参数,那样和放几个真的静态文件夹有什么区别】,然后认真看了几遍Slimframework的文档(github上面的release,顺着一个个变量看下去),有点眉目了,也算是写个存档。首先是目录配置,据说百度云用了lighttpd,我的电脑只有nginx,就先用着吧:
#begin nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root D:/Apache/; #default www root should be 'D:/Apache/;'
index index.html index.htm index.php;
}
location /welcome/ { #debug block for slimframework , which the whole framework is installed under D:/Apache/Slim-master/;
#we finally run apps under D:/Apache/welcome/;
try_files /welcome/$uri $uri/ /welcome/index.php?$request_uri;
#try_files /mySlimDirUnderWWWRoot/$uri $uri/ /mySlimDirUnderWWWRoot/index.php?$request_uri;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^/welcome/(.+\.php)(/.+)$;
#fastcgi_split_path_info ^/mySlimDirUnderWWWRoot/(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
#see http://help.slimframework.com/discussions/problems/1487-nginx-slimframework
}
error_page 404 /null.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root D:/Apache/; #root D:/Apache;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; #you SHOULD restart your server AFTER change this configuration file
fastcgi_param SCRIPT_FILENAME D:\Apache$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME [your_server_script_index]$fastcgi_script_name;
include fastcgi_params; #前提是,在php.ini 中设置了具体的doc_root (你的后台脚本的位置)
} #修改PHP.INI配置文件中的cgi.fix_pathinfo = 1,
#PHP 会修正 SCRIPT_FILENAME 为真实的文件地址,否则 PHP 将无法找到需要处理的 PHP 文件
} #end nginx.conf
按照我的电脑配置下来,www root在D:/Apache/ ,引用Slimframework的文件夹在 D:/Apache/welcome/ ,try_files配置了就可以用
Slimframework的路由了。Slimframework的api是完全面向对象的,所以在向模板传参数时,被引用的模板只需要声明传入的变量即可,不需要再次手工使用类似以下的办法:
<?php
/*this will NOT work
under slimframework*/
$foo=$_GET['foo'];
$bar=$_POST['bar'];
echo $foo;
echo $bar;
/*will throw an error when using slimframework*/
?>
因为写路由的时候,我们不需要关心传递参数用的是get或者post方法,只要在slim提供的get方法的闭包里面,将参数以键值对的形式,打包成一个数组即可:
<?php
//initial jobs
require('../Slim-master/Slim/Slim.php');
\Slim\Slim::registerAutoloader();
//here we start using slim framework
$app = new \Slim\Slim();
//GET route,then
//$app->get('/:name', function(){
//echo "Hello, $name";
//do use "" instead of ''
//});
//use get method to render a template
//模板相对位置以Slim类库位置为准,和Servlet类似
$app->get('/:name',function($name) use ($app){
$app->render('../../test/'.$name.'.html',array(''=>''));
//DO use '.' to join string and variables , instead of '+'
//second parameter must be an (empty|valid)array
//use it as key-value parameters within GET method
});
$app->group('/test/my',function() use ($app){
$app->get('/',function() use ($app){
//$app->redirect('/welcome/test/my/first',302);
//mind the '/' before the path
//redirect to whatever path it is from web root
//render用于真实文件,redirect等用于虚拟路径
//跳转时改变地址,第二参数是响应的http状态码
//从服务器根目录开始重定向,目标目录需要在服务器配置文件中进行声明
$app->redirect('/welcome/test/my/first',302);
});
$app->get('/first',function() use($app){
$app->render('../../test/foo.php',array('flag'=>'first'));
});
$app->get('/second',function() use($app){
$app->render('../../test/foo.php',array('flag'=>'second'));
});
$app->get('/third',function(){
echo "third test";
});
});
//start your application finally
$app->run();
?>
作为响应的部分,直接处理传来的变量即可,无需经过 $_GET 或者$_POST 取值:
<?php
/*foo.php*/
if($flag!=null){
echo "your input is:".$flag;
}else{
echo "no data input";
}
?>
转载请附上本文链接,谢谢。先写这么多吧,回头弄个lighttpd的地址rewrite心得,话说csdn的编辑器不太好使啊...上传了nginx配置文件的"#"注释老是被识别成别的东西,这字体大小不一也是醉了。