Table Of Contents

m

全干工程师

给phalcon注解路由加上hostname功能

实现

创建自己的路由类文件,这里以HostnameAnnotationRouter为文件名. 重写processControllerAnnotationadd 两个方法, 以下为全部代码.


<?php
/**
 * Created by PhpStorm.
 * User: meshell
 * Date: 2019-02-22
 * Time: 13:50
 */

namespace App\Router;


use Phalcon\Annotations\Annotation;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Annotations;
use Phalcon\Mvc\Router\Route;
use Phalcon\Mvc\Router\RouteInterface;

/**
 * Class HostnameAnnotationRouter
 * @package App\Services
 */
class HostnameAnnotationRouter extends Annotations
{
    protected $_hostname = null;


    public function processControllerAnnotation($handler, Annotation $annotation)
    {
        parent::processControllerAnnotation($handler, $annotation); // TODO: Change the autogenerated stub
        if ($annotation->getName() == "RoutePrefix") {
            $this->_hostname = $annotation->getNamedArgument("hostname");
        }
    }

    /**
     * @param string $pattern
     * @param null $paths
     * @param null $httpMethods
     * @param int|mixed $position
     * @return RouteInterface|Route
     */
    public function add($pattern, $paths = null, $httpMethods = null, $position = Router::POSITION_LAST)
    {
        $route = parent::add($pattern, $paths, $httpMethods, $position); // TODO: Change the autogenerated stub
        if ($this->_hostname) {
            $route->setHostname($this->_hostname);
        }
        return $route;
    }


}

使用


<?php
/**
 * Created by PhpStorm.
 * User: zhoutianliang
 * Date: 2017/5/7
 * Time: 11:41
 */

namespace App\Http\Controllers\Tutorial;

use App\Http\Controllers\Controller;
use App\Http\Exceptions\NotFoundHttpException;
use App\Models\Options;
use App\Repositories\TutorialRepository;
use App\Services\Fast;

/**
 * Class TutorialController
 * @package App\Http\Controllers\Tutorial
 * @RoutePrefix("/", hostname="(loocode\.(local|com))")
 */
class TutorialController extends Controller
{
     /**
     *
     * @Route("")
     */
    public function entryAction()
    {

    }
}

留言