吖晋 发表于 2011-9-7 11:51:32

如何判断是否存在这个Controller和Action

我一直在构想可以自定义文章的路径,就像Wp那个别名一样!可惜我又担心和Controller冲突,我就研究了一下源码.但是不知道有没有一个方法类是可以直接验证的.

源码是这样验证的:

function _validate_request($segments)
{
if (count($segments) == 0)
{
   return $segments;
}

// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments.EXT))
{
   return $segments;
}

// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments))
{
   // Set the directory and remove it from the segment array
   $this->set_directory($segments);
   $segments = array_slice($segments, 1);

   if (count($segments) > 0)
   {
    // Does the requested controller exist in the sub-folder?
    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments.EXT))
    {
   show_404($this->fetch_directory().$segments);
    }
   }
   else
   {
    // Is the method being specified in the route?
    if (strpos($this->default_controller, '/') !== FALSE)
    {
   $x = explode('/', $this->default_controller);

   $this->set_class($x);
   $this->set_method($x);
    }
    else
    {
   $this->set_class($this->default_controller);
   $this->set_method('index');
    }

    // Does the default controller exist in the sub-folder?
    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
    {
   $this->directory = '';
   return array();
    }

   }

   return $segments;
}


// If we've gotten this far it means that the URI does not correlate to a valid
// controller class.We will now see if there is an override
if ( ! empty($this->routes['404_override']))
{
   $x = explode('/', $this->routes['404_override']);

   $this->set_class($x);
   $this->set_method(isset($x) ? $x : 'index');

   return $x;
}


// Nothing else to do at this point but show a 404
show_404($segments);
}



所以其实有没有一个方法类是验证的?我不想重复造轮子!哈哈!

ming 发表于 2011-9-7 17:15:49

你的意思是说检测ci中有没一个特定名字的controller?

吖晋 发表于 2011-9-7 17:18:24

ming 发表于 2011-9-7 17:15 static/image/common/back.gif
你的意思是说检测ci中有没一个特定名字的controller?

大概的意思就是我有一个控制器叫 Post 吧...那么我怎么判断他是否存在?????

其实我只是求一个更加简单的方法!!!!!

ming 发表于 2011-9-7 17:26:16

判断它是已经定义了还是已经在运行?

吖晋 发表于 2011-9-7 17:35:21

ming 发表于 2011-9-7 17:26 static/image/common/back.gif
判断它是已经定义了还是已经在运行?

不用判断什么运行之类的..其实我就是想用户可以自定义一个栏目的别名

xx.com/别名

这样...但如果这个别名和Post冲突了就不能修改这个别名!

dde333 发表于 2011-9-7 17:38:14

学习学习!

ming 发表于 2011-9-7 17:46:57

。。。有一个可以很简单的方法,在controller文件夹下建立一个custom之类的文件夹,在里面放用户自定义的别名就可以了,当要读取自定义名的时候在这个文件夹下读取就可以了

吖晋 发表于 2011-9-7 19:43:30

ming 发表于 2011-9-7 17:46 static/image/common/back.gif
。。。有一个可以很简单的方法,在controller文件夹下建立一个custom之类的文件夹,在里面放用户自定义的别 ...

如果我动态设置的呢???我每篇文章就一个别名..那怎么弄?

其实我觉得..你一直没明白我问的问题..哈哈!!!

ming 发表于 2011-9-8 10:59:37

哈哈,我也这么认为{:soso_e136:}

Hex 发表于 2011-9-8 11:22:58

这个只能是扩展Router 类,路由是 CI 里 URL 的处理中心,由这里分发给各个控制器的方法。
页: [1] 2
查看完整版本: 如何判断是否存在这个Controller和Action