Python 的輕鬆閲讀
Python 的輕鬆閲讀

寫寫 Python,還有關於 Python 延伸的項目

Codeigniter 3 + twig + API 快速建立一個 CMS

這是已經做好的前後台

本機的環境

  • Windows 11Laragon,模擬 php, nginx, apache, mariaDB 的環境
  • 目前使用 nginx,自己的習慣就是 nginx
  • PHPStorm,寫 PHP 的利器之一瀏覽器:Edge 或者 Chrome,筆者選擇 Edge 主要跑的就是順暢

基本三個就達成開發環境了,依照自己的環境架設起來就好了

Codeigniter 3 先安裝起來

目前最新版本是 3.1.11,可以跑在 7.4,目前是沒有問題

按這裏下載

下載完成直接解壓縮,放到自己的環境底下

環境配置

說明整個資料夾基本的配置

\
|- application
  \
  |- cache
  |- config
  |- controllers
  |- core
  |- helpers
  |- hooks
  |- language
  |- libraries
  |- log
  |- models
  |- trird_party
  |- view
  .htaccess
  index.html
|- system
| composer.json
| index.php

以下配置檔案

  • application\config
  • database.php
// 資料庫的配置是依照每個人的設定
'hostname' => 'localhost',
'username' => 'root',
'password' => 'hezrid5',
'database' => 'ideaee',
'dbdriver' => 'mysqli',
  • application\config
    autoload.php

修改的有以下的設定,model 只是這個案件需要,所以事先載入

$autoload['libraries'] = array('database', 'session', 'form_validation', 'twig', 'encryption');

$autoload['helper'] = array('url', 'file');

$autoload['model'] = array('System_model', 'Contact_model', 'Ga_model');
  • application\config
    config.php
// 換網站都不會有影響,不用在手寫網址了
$config['base_url'] = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'];

// 這個設成空白
$config['index_page'] = '';

// hooks 後面會說到他的用法,主要是後端登入需要的 hook
$config['enable_hooks'] = TRUE;

// 開啓 composer 的方式
$config['composer_autoload'] = 'vendor/autoload.php';

// 加密會用到,順便 key 上去
$config['encryption_key'] = 'dLC3UzpFoJwr5n7Ed13PNj7kVOyu4WzI';

  • application\config
    routes.php
// 我的配置
$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['about/(:num)'] = 'about/num/$1';
$route['news/(:num)'] = 'news/num/$1';
$route['service/(:num)'] = 'service/num/$1';
$route['knowledge/(:num)'] = 'knowledge/num/$1';

建立 API 的方法

建立的方式,這個算是簡單易懂的方法

請看這邊設定,我就不詳細說明

這樣就基本設置,就沒有問題了

簡單說明我做的網站

  • 後臺總共分為 API 以及後臺程式
  • API 使用 REST_Controller.php 在使用
  • 前臺的部分,主要透過 model 來呈現的方式

使用 hooks 來做後臺的登入驗證

  • application/config
  • hooks.php

這裏使用 hooks 來確認使用者是否登入

$hook['post_controller_constructor'] = array(
    'class'    => 'ManageAuth',
    'function' => 'auth',
    'filename' =>'ManageAuth.php',
    'filepath' => 'hooks'
);
  • application/hooks
    ManagerAuth.php

這是 hooks 的路徑裏面的

<?php
/** 后台权限拦截钩子 * @link http://www.php-chongqing.com * @author bing.peng *  */
class ManageAuth {
    private $CI;
    public function __construct()
    {
        $this->CI = &get_instance();
    }
    //权限认证
    public function auth()
    {
        $this->CI->load->helper('url');
            if ( preg_match("/backend.*/i", uri_string()) ) {
                // 需要进行权限检查的URL
                $this->CI->load->library('session');
                if( !$this->CI->session->userdata('admin_login') ) {
                    // 用户未登陆
                    redirect('/login');
                    return;
                }
        }
     }
}

twig 樣板引擎

先讓它安裝起來

composer require "twig/twig:~2.0"

直接寫 twig library

  • application/libraries
    Twig.php
<?php
// libraries/Twig.php

class Twig
{
    private $ci;
    private $twig;

    public function __construct()
    {
        $this->ci =& get_instance();

        $loader = new Twig_Loader_Filesystem(VIEWPATH);
        $this->twig = new Twig_Environment($loader,
            array('cache' => APPPATH.'/cache', 'debug' => true));


        $this->twig->addGlobal('baseUrl', base_url());
        $this->twig->addFunction(new Twig\TwigFunction('setValue', 'set_value'));
        $this->twig->addFunction(new Twig\TwigFunction('formError', 'form_error'));
    }

    public function output($template, $data=[])
    {
        $this->ci->output->set_output(
            $this->twig->render($template . '.html', $data)
        );
    }
}

這樣整個環境都有了 接下來,依照這個設計,基本就有 CMS 的功能了

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…

发布评论