Convert Dashes to Underscores in URLs automatically in CI4

dashes to underscore in ci4 url

Step 1: Create a Custom Router

Create a custom router that converts dashes to underscores in controller methods.

File Path: app/Controllers/Router.php

Step 2: Modify Routes.php

In app/Config/Routes.php, include the Router::convertDashToUnderscore method to enable the mapping.

Update Your Routes.php

Step 3: Test the URL

Now, accessing http://yourdomain.com/admin/configurator/configurator/order-partial-payment will automatically call the order_partial_payment method in your controller.

Notes:

  1. Security Implications:
    • Be cautious when enabling auto-routing ($routes->setAutoRoute(true)), as it allows users to call any public method in your controllers.
    • It’s recommended to explicitly define routes to prevent unintended access.
  2. Debugging:
    • Check routes using php spark routes to verify if your route is being mapped correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *