Giter VIP home page Giter VIP logo

codeigniter-attribute-routes's Introduction

Custom Routing Library for CodeIgniter 4

This library enhances the routing system of CodeIgniter 4 by introducing PHP 8 Attributes. It allows defining routes directly in controller methods, thereby simplifying the routing process and reducing redundancy. The library also supports multiple controller namespaces and integrates middleware easily with routes.

Features

  • Define routes using PHP 8 Attributes.
  • Support for GET, POST, PUT, DELETE, PATCH, and OPTIONS methods.
  • Regular expression patterns in routes.
  • Filter integration with routes.
  • Multiple controller namespaces.

Installation

  1. Install via Composer

    Add the library to your CodeIgniter 4 project using Composer:

    composer require galihlasahido/codeigniter-attributeroutes
  2. Update the Bootstrap File

    use Galihlasahido\Attributeroutes\Router\CustomRouter;
    use Config\Services;
    
    $routes = Services::routes();
    $customRouter = new CustomRouter($routes, Services::request(), ['App\\Controllers']);
    $customRouter->initialize();
    
    Services::injectMock('router', $customRouter);

    Adjust the namespaces in the array as per your project structure.

Usage

Defining Routes

Use attributes to define routes directly in the controller methods:

namespace App\Controllers;

use Galihlasahido\Codeigniter\Attributeroutes\Attributes\GetRoute;
use Galihlasahido\Codeigniter\Attributeroutes\Attributes\PostRoute;

class MyController {
    #[GetRoute('/', ['filter'=> 'MyFilter'])]
    public function index() {
        return "GET method with for index";
    }

    #[GetRoute('/test-get/(\d+)')]
    public function testGetMethod($id) {
        return "GET method with ID: $id";
    }

    #[PostRoute('/test-post')]
    public function testPostMethod() {
        return "POST method";
    }

}

Usage with different namespace

  1. Update the Bootstrap File

    Modify your app/Config/Routes.php to use the custom router:

    use Galihlasahido\Attributeroutes\Router\CustomRouter;
    use Config\Services;
    
    $routes = Services::routes();
    $customRouter = new CustomRouter($routes, Services::request(), ['App\\Controllers', 'Modules\\Dashboard\\Controllers']);
    $customRouter->initialize();
    
    Services::injectMock('router', $customRouter);

    Modify your app/Config/Autoload.php to use the custom router, add this code to the class

    public function __construct() {
        parent::__construct();
        
        foreach(glob(ROOTPATH . 'modules/*', GLOB_ONLYDIR) as $item_dir) {
            $explode = explode(DIRECTORY_SEPARATOR, $item_dir);
            if (file_exists($item_dir)) {
                $this->psr4['Modules\\'.end($explode)] = ROOTPATH . 'modules/'.end($explode);
            }	
        }
    }

    To use the class you can build a class in the following folder

    └── Codeigniter project/
        └── Modules/
            ├── Controllers/
            │   └── Dashboard.php
            ├── Views
            ├── Config
            └── Models
    
    <?php
    
    namespace Modules\Dashboard\Controllers;
    
    use Galihlasahido\Codeigniter\Attributeroutes\Attributes\GetRoute;
    
    class Dashboard extends BaseController {
        
        public function __construct() {
        }
    
        #[GetRoute('/test-dashboard')]
        public function index() {
            return view('Modules\Dashboard\Views\main');
        }
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.