PHP code example of galihlasahido / codeigniter-attributeroutes

1. Go to this page and download the library: Download galihlasahido/codeigniter-attributeroutes 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/ */

    

galihlasahido / codeigniter-attributeroutes example snippets


    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);
    

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";
    }

}

    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);
    

    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);
            }	
        }
    }
    

    

    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');
        }
    }
    

    └── Codeigniter project/
        └── Modules/
            ├── Controllers/
            │   └── Dashboard.php
            ├── Views
            ├── Config
            └── Models