Reputation: 987
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
Upvotes: 2
Views: 2396
Reputation: 987
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, "\n". $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, "\n". $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, "\n". $page_content);
fclose($page);
}
Upvotes: 3