drze7794 2017-04-06 05:14
浏览 36

在CodeIgniter中自动创建视图和控制器

I'm using codeigniter and i have a project with a lot of pages. Each page is consist of controller + view. Maybe there is a way to automatically create pages and controllers base on a template? maybe codeigniter has like a batch file option that detects changes in a specific table (pages table for example) in database and create the controller + view files accordingly?

Thanks

  • 写回答

1条回答 默认 最新

  • doubei8541 2017-04-10 21:42
    关注

    Found a way to create a 'batch' file to create model/view/controller

    Enjoy.

    I used Codeigniter's helper to do so.

    From another controller load the helper

        $this->load->helper('pages_creator');
        create_new_page('test', 'Test', 'Test');
    

    Here is the helper pages_creator_helper.php

      <?php
    
      function create_new_page($page_name, $class_name, $controller_name){
    
      // Create Controller
      $controller = fopen(APPPATH.'controllers/'.$controller_name.'.php', "a")
      or die("Unable to open file!");
    
      $controller_content ="<?php
      defined('BASEPATH') OR exit('No direct script access allowed');
    
      class $class_name extends MY_Controller  {
    
      public function __construct()
      {
        parent::__construct();
    
       }
      public function index()
       {
        \$this->data['site_title'] = '$page_name';
        \$this->twig->display('$page_name',\$this->data);
    
       }
    
       }";
      fwrite($controller, "
    ". $controller_content);
      fclose($controller);
    
      // Create Model
      $model = fopen(APPPATH.'models/'.$class_name.'_model'.'.php', "a") 
      or die("Unable to open file!");
    
       $model_content ="<?php if ( ! defined('BASEPATH')) exit('No direct script 
       access allowed');
    
       class ".$class_name."_model"." extends CI_Model
      {
      function __construct()
      {
        // Call the Model constructor
        parent::__construct();
      }
    
      }
      ";
      fwrite($model, "
    ". $model_content);
      fclose($model);
    
      // Create Twig Page
    
      $page = fopen(APPPATH.'views/'.$page_name.'.twig', "a") or die("Unable to    
      open file!"); 
    
      $page_content ='{% extends "base.twig" %}
      {% block content %}
    
      <div class="row">
        <div class="col-md-12">
            <h1>TO DO {{ site_title }}</h1>
    
        </div>
        <!-- /.col -->
      </div>
    
       {% endblock %}';
      fwrite($page, "
    ". $page_content);
      fclose($page);
       }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题