Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: $routes->cli() accessible via web browser if autoroute is true #2704

Closed
samsonasik opened this issue Mar 14, 2020 · 4 comments · Fixed by #2707 or #5877
Closed

Bug: $routes->cli() accessible via web browser if autoroute is true #2704

samsonasik opened this issue Mar 14, 2020 · 4 comments · Fixed by #2707 or #5877
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Milestone

Comments

@samsonasik
Copy link
Member

For example, I have the following cli route:

// set auto route to true
$routes->setAutoRoute(true); 

// set "hello" cli route
$routes->cli('hello', 'Commands\Hello::index');

and I have the controller:

<?php
namespace App\Controllers\Commands;

use App\Controllers\BaseController;

class Hello extends BaseController
{
    public function index()
    {
        echo 'Hello';
    }
}

then, I open it via web browser, it still accesible via : http://ci4.local/commands/hello , it still accessible.

CodeIgniter 4 version
4.0.2

Expected behavior, and steps to reproduce if appropriate

By define cli(), it should decline the http access into it. The documentation doesn't say it require autoroute to be false:

You can create routes that work only from the command-line, and are inaccessible from the web browser, with the cli() method

Context

  • OS: OSX
  • Web server Apache
  • PHP version 7.4
@samsonasik samsonasik added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 14, 2020
@MGatner MGatner added this to the 4.0.* milestone Mar 15, 2020
samsonasik added a commit to samsonasik/CodeIgniter4 that referenced this issue Mar 15, 2020
… accessible via web browser even autoroute is true
@samsonasik
Copy link
Member Author

I've created PR #2707 for it

samsonasik added a commit to samsonasik/CodeIgniter4 that referenced this issue Mar 19, 2020
… accessible via web browser even autoroute is true
lonnieezell added a commit that referenced this issue Mar 22, 2020
Fix #2704: ensure route registered via $routes->cli() not accessible via web browser even autoroute is true
@kenjis
Copy link
Member

kenjis commented Apr 11, 2022

app/Config/Routes.php:

$routes->setAutoRoute(true);
 
$routes->cli('hello/(:segment)', 'Home::$1');

We can access:
http://localhost:8080/home
http://localhost:8080/home/index

@lonnieezell
Copy link
Member

@kenjis Auto-routing is a different bag of worms. Unfortunately, it's how CodeIgniter apps have always worked, too.

@Kr3m
Copy link

Kr3m commented Mar 25, 2023

I should add this for anyone who might stumble across it that needs a solution.

if(is_cli()) {
 echo 'Hello';
}

Then you could create an else block to redirect them to a 404.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
5 participants