Phalcon路由模式快速配置

初次接触Phaclon框架,查看了关于路由的说明和nginx的相关配置说明,介绍说明稍比较分散,初次接触这个东西不容易get到全部信息,在路由这块学习配置起来需要花费大量时间去摸索,对此稍作进行整理,Phaclon框架默认情况下,URI信息是从$_GET["_url"]变量获取的,这被Rewrite-Engine传递给Phalcon,也可以使用$_SERVER['REQUEST_URI']如果需要:

(一)以下是文档介绍及配置相关内容:

1.Config/router.php

PHP 
$router = $di->getRouter();
// ...//使用$_GET["_url"](默认,可以不写)
$router->setUriSource($router::URI_SOURCE_SERVER_REQUEST_URI);
//使用$_SERVER[“REQUEST_URI”] 
$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI);

2.关于Nginx的配置
1)使用$_GET['_url'] 模式作为路由来源($router - > setUriSource (Router :: URI_SOURCE_GET_URL);):

server {
    listen      80;
    server_name localhost.dev;
    root        /var/www/phalcon/public;
    index       index.php index.html index.htm;
    charset     utf-8;

    location / {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }
    location ~ \.php {
        fastcgi_pass  unix:/run/php-fpm/php-fpm.sock;
        fastcgi_index /index.php;

        include fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ /\.ht {
        deny all;
    }
   }

2)使用$_SERVER['REQUEST_URI'] 模式作为路由来源($router - > setUriSource (Router :: URI_SOURCE_SERVER_REQUEST_URI );):

server {
    listen      80;
    server_name localhost.dev;
    root        /var/www/phalcon/public;
    index       index.php index.html index.htm;
    charset     utf-8;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        try_files     $uri =404;

        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index /index.php;

        include fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
 }

(二)感觉配置起来比较繁琐需要区分那种模式来调整不同nginx server配置,以下为个人尝试内容:
1)尝试将nginx统一配置为

server {
    listen      80;
    server_name localhost.dev;
    root        /var/www/phalcon/public;
    index       index.php index.html index.htm;
    charset     utf-8;

  location / {
        index index.php;
        if (-f $request_filename) {
             expires max;
             break;
            }
        if (!-e $request_filename) {
             rewrite ^/(.*)$ /index.php/$1 last;
           }
    }

    location ~ \.php$ {
        try_files     $uri =404;

        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index /index.php;

        include fastcgi_params;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
  }

2)使用不同在模式在系统中的配置

① 使用_url模式可以在入口文件(index.php)添加

     $_GET['_url'] = $_SERVER['REQUEST_URI'];

② 使用$_SERVER[‘REQUEST_URI’]模式可以在路由配置文件(Config/router.php)中增加

    $router - > setUriSource (Router :: URI_SOURCE_SERVER_REQUEST_URI );

即可。

你可能感兴趣的:(php)