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

即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值