美文网首页
xiaocms进阶教程:为表单建立list模板(自定义contr

xiaocms进阶教程:为表单建立list模板(自定义contr

作者: Swerve | 来源:发表于2017-04-06 11:16 被阅读0次

    通过看源码我们能够发现xiaocms的入口都是index.php 不通过index.php进入的页面是不会执行模板语言的。而index.php?c=这个地址中c参数即为controller的位置。
    首先我们先来看一下系统的控制器,进入core>controller>index.php 这里就是系统默认自带的action和controller分配的地方了。我们进入indexAction函数

        public function indexAction() {
        if($this->get('catdir') || $this->get('catid')){//如果url中包含catid,那么就是栏目页面。
            $catid  = (int)$this->get('catid');
            if(!empty($catid)) {
            $category    = $this->category_cache[$catid];
            }
            else if ($this->get('catdir')) {
            $category_dir = get_cache('category_dir');
            $catid = $category_dir[$this->get('catdir')];
            $category = $this->category_cache[$catid];
            }
            if (empty($category)) {
                header('HTTP/1.1 404 Not Found');
                $this->show_message('当前栏目不存在');
            }
            $category['page']   = (int)$this->get('page') ? (int)$this->get('page') : 1;
            if($category['islook'] && !$this->member_info) $this->show_message('当前栏目游客不允许查看');
            $category['cat'] = $category;
            $this->view->assign($category);
            $this->view->assign($this->listSeo($category, $category['page'] ));
            if ($category['typeid'] == 1) //根据typeid来判断栏目具体是那种类型的,然后分配不同的模板。
                $this->view->display($category['listtpl']);//这个方法很重要,对于我们自定义controller和使用自己的模板来说就靠这个方法了。
    //如果我们在display参数中填入自己的模板,那么系统就会调用我们自己的模板了。
            else if ($category['typeid'] == 2) 
                $this->view->display($category['pagetpl']);
            else if  ($category['typeid'] == 3) {
                header('Location: ' . $category['http']);
            }
        }
        else if ($this->get('id')){//如果url中包含的是id信息,那么就是详情页。
            $id    = (int)$this->get('id');
            $content  = $this->db->setTableName('content')->find($id);
            if (empty($content)) {
                header('HTTP/1.1 404 Not Found');
                $this->show_message('不存在此内容!');
            }
            if (empty($content['status'])) $this->show_message('此内容正在审核中不能查看!');
            $category   = $this->category_cache[$content['catid']];
            if($category['islook'] && !$this->member_info) $this->show_message('当前栏目游客不允许查看');
            $content_add = $this->db->setTableName($category['tablename'])->find($id);
            $content_add = $this->handle_fields($this->content_model[$content['modelid']]['fields'], $content_add);
            $content  = $content_add ? array_merge($content,$content_add) : $content;
            $content['page']   = (int)$this->get('page') ? (int)$this->get('page') : 1;
            if (strpos($content_add['content'], '[XiaoCms-page]') !== false) {//分页
                $pdata  = array_filter ( explode('[XiaoCms-page]', $content_add['content']) );
                $pagenumber = count($pdata);
                $content['content'] = $pdata[$content['page']-1];
                $pageurl = $this->view->get_show_url($content, 1);
                $pagelist = xiaocms::load_class('pager');
                $pagelist = $pagelist->total($pagenumber)->url($pageurl)->num(1)->hide()->page($content['page'])->output();
                $this->view->assign('pagelist', $pagelist);
            }
    
            $content['cat'] = $category;//上一篇,下一篇
            $prev_page = $this->db->setTableName('content')->order('id DESC')->getOne(array('id<?', 'modelid=' .$content['modelid'], 'status!=0'), $id);
            if ($prev_page) $prev_page['url'] =  $this->view->get_show_url($prev_page);
            $next_page = $this->db->setTableName('content')->order('id ASC')->getOne(array('id>?', 'modelid=' .$content['modelid'] , 'status!=0'), $id);
            if ($next_page) $next_page['url'] =  $this->view->get_show_url($next_page);
            $this->view->assign($content);
            $this->view->assign($this->showSeo($content, $content['page']));
            $this->view->assign(array(
                'prev_page' => $prev_page,
                'next_page' => $next_page,
            ));
            $this->view->display($category['showtpl']);
        }
        else {//如果url中没有这些信息,就是主页。
            $this->view->assign(array(
                'index'           => 1,
                'site_title'       => $this->site_config['site_title'],
                'site_keywords'    => $this->site_config['site_keywords'], 
                'site_description' => $this->site_config['site_description'],
            ));
            $this->view->display('index.html');
        }
        }
    

    相关文章

      网友评论

          本文标题:xiaocms进阶教程:为表单建立list模板(自定义contr

          本文链接:https://www.haomeiwen.com/subject/egwfattx.html