PHP code example of takuya-motoshima / codeigniter-extension

1. Go to this page and download the library: Download takuya-motoshima/codeigniter-extension library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

takuya-motoshima / codeigniter-extension example snippets


        $route['default_controller'] = 'users/login';
        

        const SESSION_NAME = 'session';
        

        use \X\Annotation\AnnotationReader;
        use \X\Util\Logger;

        $hook['post_controller_constructor'] = function() {
          if (is_cli())
            return;
          $CI =& get_instance();
          $meta = AnnotationReader::getAccessibility($CI->router->class, $CI->router->method);
          $loggedin = !empty($_SESSION[SESSION_NAME]);
          $current = lcfirst($CI->router->directory ?? '') . lcfirst($CI->router->class) . '/' . $CI->router->method;
          $default = '/users/index';
          $allowRoles = !empty($meta->allow_role) ? array_map('trim', explode(',', $meta->allow_role)) : null;
          if (!$meta->allow_http)
            throw new \RuntimeException('HTTP access is not allowed');
          else if ($loggedin && !$meta->allow_login)
            redirect($default);
          else if (!$loggedin && !$meta->allow_logoff)
            redirect('/users/login');
          else if ($loggedin && !empty($allowRoles)) {
            $role = $_SESSION[SESSION_NAME]['role'] ?? '';
            if (!in_array($role, $allowRoles) && $default !== $current)
              redirect($default);
          }
        };

        $hook['pre_system'] = function () {
          $dotenv = Dotenv\Dotenv::createImmutable(ENV_DIR);
          $dotenv->load();
          set_exception_handler(function ($e) {
            Logger::error($e);
            show_error($e->getMessage(), 500);
          });
        };
        

    $_SESSION['user'] = ['name' => 'John Smith'];
    
sh
            sudo systemctl restart nginx
            sudo systemctl restart php-fpm