在Application子类中获取url操作

时间:2015-08-03 21:01:39

标签: php url-routing

此功能位于Application class> mvc> php

private function splitUrl()
{
    if (isset($_GET['url'])) {

        // split URL
        $url = trim($_GET['url'], '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);
        $this->url_controller = isset($url[0]) ? $url[0] : null;
        $this->url_action = isset($url[1]) ? $url[1] : null;
        unset($url[0], $url[1]);
        $this->url_params = array_values($url);
    }
}

新修改

我有这个班级

class Page extends Controller
{
    public function __construct(){

            //echo parent::splitUrl()->this->url_action;

         echo parent::$this->url_action;

    }
    public function index()
    {   
   // removed lins
    }
}

如何在扩展函数中获得 $this->url_action

1 个答案:

答案 0 :(得分:0)

字段url_action应受到保护(或公开,但不建议这样做)。所以你可以在子类中使用它。

见下面的假冒代码:

在你的父母:

class Foo
{
   protected $url_action = 'something'; // or set it via setter
}

儿童班:

class FooChild extends Foo // Now, child can access protected (and public) fields of its parent!
{
   public function getFromParent()
   {
       return $this->url_action; // or what you want
   }

}