1. MVC
1.1 控制器调用models
$Live = new Live();
$showData = $Live->getDataPage("status=1 and old_source = 'jgy' ",'id DESC',$pagesize,$index);
1.2 接参数
// 检查请求是否为POST
if ($this->request->isPost() == true) {
// 获取POST数据
$customerName = $this->request->getPost("name");
$customerBorn = $this->request->getPost("born");
}
// get
$pagesize = $this->request->get("pagesize");
$index = $this->request->get("index");
一般情况下,GET/POST请求获取参数:
$this->request->get(参数名);
$this->request->getPost("参数名")
路由模式下route获取参数要用dispatcher->getParam();
route下定义好了参数名称可以直接通过参数名称来获取:
this->dispatcher->getParam("参数名");
route没有定义好名称,只是规则中写了:params做匹配,可以在控制器中按顺序来获取:
class NewsController extends Controller {
public function showAction($id, $testParam)
{
echo $id, '|' , $testParam;
}
}
1.3 html中请求并携带参数
<?php echo $this->url->get(['for' => 'frontend', 'controller' => 'common', 'action' => 'showimage']); ?>
携带参数
<?php echo $this->url->get(['for' => 'frontend', 'controller' => 'live', 'action' => 'datas'],['pagesize'=>10]); ?>
// http://test.com/api//live/datas?pagesize=10
2. url 定制路由
2.1 为 url 定制路由
默认自动解析/:controller/:action/:params模式:
在实例化时,不加false参数:
$router = new Router();
url将会自动进行/:controller/:action/:params参数的解析, 比如https://www.goozp.com/login将会解析成Login controller下的默认action。
当使用路由时,保留默认解析模式有时候会导致解析混乱,比较建议采用完全自定义路由模式。
完全自定义路由,在new时加上false:
$router = new Router(false);
不自动解析/:controller/:action/:params这些规则, 具体的路由匹配规则自己来编写,例如:
$router->add('/login',
[
'module' => 'admin',
'controller' => 'login',
'action' => 'index',
]
)->setName('login');
这样不会因为自动解析而导致url混乱,但是所有url都要自己来定制路由规则。
2.2 URL重定向:
//设置一个内部跳转
$this->response->redirect( 'posts/index' );
// 外部跳转url
$this->response->redirect( 'http://blog.csdn.net/q718330882', true );
// 设置跳转 http状态
$this->resopnse->redirect( 'http://csd.net' , true , 301 );
___重定向不会禁用视图组件。因此,如果你想从一个controller/action重定向到另一个controller/acton上,视图将正常显示。当然,你也可以使用 $this->view->disable() 禁用视图输出。
3. Config 中 baseURI 的正确设置
因为有Apache+.htaccess文件重写规则 或者 nginx配置到public/index.php的重写规则,我们不需要项目中的url带有/publc/index.php。
但是默认是指到了/public/index.php中(比如$_SERVER["PHP_SELF"]获取从根目录到当前页面本身的路径); 所以,如果有Apache重写规则或者nginx配置到public/index.php的重写配置,我们需要把url设置为不带public/index.php的,于是就有了官方的这个设置:
使用 $_SERVER["PHP_SELF"],并且正则去除/public/index.php
'baseUri' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
这是动态写法,这种写法的问题在于 $_SERVER["PHP_SELF"] 的不确定性,返回的值将根据 Apache 或 nginx 配置的 root,是否配置host或者域名,$_SERVER["PHP_SELF"]会有不同的返回值。这样的话上面写法前面的正则并不是全部兼容的,所以这样写调试起来就稍麻烦。
简单一点,用静态写法:
设置host或者配置域名
'baseUri' => '/',
如果是想在localhost下直接打开,则需要加上项目外层目录名,例如:'baseUri' => '/zphal/',
这样的话,我们在定义url服务的时候只需要把这个定义的配置传进去:
$di->setShared('url', function () {
$config = $this->getConfig();
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri); // baseUri
return $url;
});
以上写法的WebServer配置:
Apache:
.hatccess按照官方配置就可以;配置host时配置到public下或者public外面一层的项目根目录也可以:
<VirtualHost *:80>
DocumentRoot "D:\phpStudy\WWW\zPhal\public"
ServerName goozp.com
ServerAlias
<Directory "D:\phpStudy\WWW\zPhal\public">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Nginx
大概的配置如下,配置到public下,并定义rewrite规则:
server {
listen 80;
server_name www.goozp.com goozp.com;
root /data/www/zPhal/public;
index index.php index.html index.htm;
charset utf-8;
client_max_body_size 100M;
fastcgi_read_timeout 1800;
location / {
# Matches URLS `$_GET['_url']`
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_pass php-fpm:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED /data/www/zPhal/public/$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME /data/www/zPhal/public/$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
}
4. 数据全局化的两种方法:
(1)session $this->session->set(‘auth’,array(
‘number’ => $info->number,
‘name’ => $info->name,
‘permission’=>$info->permission));
(2)persistent持久化, $this->persistent->acl = $acl
5. 循环调度
循环调度将会在分发器执行,直到没有action需要执行为止。
$this->dispatcher->forward(array("controller" => "users", "action" => "signin"));
对于“forwards”转发的次数没有限制,只要不会形成循环重定向即可,否则就意味着 你的应用将会停止(译者注:如果浏览器发现一个请求循环重定向时,会终止请求)。 如果在循环调度里面没有其他action可以分发,分发器将会自动调用由 Phalcon\Mvc\View 管理的MVC的视图层。