codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: named routes doesn't work in CLI #8027

Closed michalsn closed 1 year ago

michalsn commented 1 year ago

PHP Version

8.2

CodeIgniter4 Version

4.4.1 and develop

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli

Database

irrelevant

What happened?

I'm not able to use any named route when running CLI.

Steps to Reproduce

In Routes.php I declared a named route:

$routes->get('sample-url-1', 'HomeController::index', ['as' => 'sample1']);

And then, I created a sample command:

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class Sample extends BaseCommand
{
    /**
     * The Command's Group
     *
     * @var string
     */
    protected $group = 'Sample';

    /**
     * The Command's Name
     *
     * @var string
     */
    protected $name = 'sample:command';

    /**
     * The Command's Description
     *
     * @var string
     */
    protected $description = 'Sample command';

    /**
     * The Command's Usage
     *
     * @var string
     */
    protected $usage = 'sample';

    /**
     * The Command's Arguments
     *
     * @var array
     */
    protected $arguments = [];

    /**
     * The Command's Options
     *
     * @var array
     */
    protected $options = [];

    /**
     * Actually execute a command.
     *
     * @param array $params
     */
    public function run(array $params)
    {
        CLI::write('site_url: ' . site_url('sample1'), 'blue');
        CLI::write('url_to: ' . url_to('sample1'), 'blue');
        CLI::write('route_to: ' . route_to('sample1'), 'blue');
    }
}

Now, when I run php spark sample:command only the first CLI::write is displayed. Then I get the error:

{
    "title": "CodeIgniter\\Router\\Exceptions\\RouterException",
    "type": "CodeIgniter\\Router\\Exceptions\\RouterException",
    "code": 500,
    "message": "The route for \"sample1\" cannot be found.",
    "file": "/Users/michalsn/Sites/ci441/vendor/codeigniter4/codeigniter4/system/Helpers/url_helper.php",
    "line": 489,
    ...

Expected Output

Displaying all URLs

Anything else?

No response

iamsyh commented 1 year ago

I am having the same issue.

They also don't work when running controllers via cli like so:

php index.php generate_sitemaps

Error in terminal:

Call to undefined method CodeIgniter\HTTP\URI::baseUrl()

at SYSTEMPATH/Helpers/url_helper.php:54
Backtrace:
  1    APPPATH/Controllers/Public/Sitemap/Sitemap_Generate.php:21
       base_url()

  2    SYSTEMPATH/CodeIgniter.php:919
       App\Controllers\Public\Sitemap\Sitemap_Generate()->index()

  3    SYSTEMPATH/CodeIgniter.php:494
       CodeIgniter\CodeIgniter()->runController(Object(App\Controllers\Public\Sitemap\Sitemap_Generate))

  4    SYSTEMPATH/CodeIgniter.php:353
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

  5    FCPATH/index.php:79
       CodeIgniter\CodeIgniter()->run(
kenjis commented 1 year ago
$ php spark sample:command

CodeIgniter v4.2.12 Command Line Tool - Server Time: 2023-10-10 20:34:18 UTC-05:00

site_url: http://localhost:8080/index.php/sample1
url_to: http://localhost:8080/index.php/sample-url-1
route_to: /sample-url-1
$ php spark sample:command

CodeIgniter v4.3.8 Command Line Tool - Server Time: 2023-10-11 01:35:57 UTC+00:00

site_url: http://localhost:8080/index.php/sample1

[CodeIgniter\Router\Exceptions\RouterException]

The route for "sample1" cannot be found.

at SYSTEMPATH/Helpers/url_helper.php:599

Backtrace:
  1    SYSTEMPATH/Helpers/url_helper.php:599
       CodeIgniter\Router\Exceptions\RouterException::forInvalidRoute('sample1')

  2    APPPATH/Commands/Sample.php:60
       url_to('sample1')

  3    SYSTEMPATH/CLI/Commands.php:65
       App\Commands\Sample()->run([])

  4    SYSTEMPATH/CLI/Console.php:37
       CodeIgniter\CLI\Commands()->run('sample:command', [])

  5    ROOTPATH/spark:97
       CodeIgniter\CLI\Console()->run()
kenjis commented 1 year ago

On develop:

$ php spark sample:command

CodeIgniter v4.4.1 Command Line Tool - Server Time: 2023-10-11 01:39:42 UTC+00:00

site_url: http://localhost:8080/index.php/sample1

[CodeIgniter\Router\Exceptions\RouterException]

The route for "sample1" cannot be found.

at SYSTEMPATH/Helpers/url_helper.php:489

Backtrace:
  1    SYSTEMPATH/Helpers/url_helper.php:489
       CodeIgniter\Router\Exceptions\RouterException::forInvalidRoute('sample1')

  2    APPPATH/Commands/Sample.php:60
       url_to('sample1')

  3    SYSTEMPATH/CLI/Commands.php:65
       App\Commands\Sample()->run([])

  4    SYSTEMPATH/CLI/Console.php:44
       CodeIgniter\CLI\Commands()->run('sample:command', [])

  5    ROOTPATH/spark:102
       CodeIgniter\CLI\Console()->run()
kenjis commented 1 year ago

@iamsyh Cannot reproduce with develop branch.

$ php public/index.php /
site_url: http://localhost:8080/index.php/sample1
url_to: http://localhost:8080/index.php/sample-url-1
route_to: /sample-url-1
diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index fc4914a692..f6d3dae35e 100644
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -6,3 +6,5 @@ use CodeIgniter\Router\RouteCollection;
  * @var RouteCollection $routes
  */
 $routes->get('/', 'Home::index');
+
+$routes->get('sample-url-1', 'HomeController::index', ['as' => 'sample1']);
diff --git a/app/Config/Routing.php b/app/Config/Routing.php
index 8d3c773157..b9bf71d815 100644
--- a/app/Config/Routing.php
+++ b/app/Config/Routing.php
@@ -88,7 +88,7 @@ class Routing extends BaseRouting
      *
      * If FALSE, will stop searching and do NO automatic routing.
      */
-    public bool $autoRoute = false;
+    public bool $autoRoute = true;

     /**
      * If TRUE, will enable the use of the 'prioritize' option
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 5934333309..e2f0707959 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,8 @@ class Home extends BaseController
 {
     public function index(): string
     {
-        return view('welcome_message');
+        return 'site_url: ' . site_url('sample1') . PHP_EOL
+            . 'url_to: ' . url_to('sample1') . PHP_EOL
+            . 'route_to: ' . route_to('sample1') . PHP_EOL;
     }
 }
kenjis commented 1 year ago

I sent PR #8028. Please check.

iamsyh commented 1 year ago

@iamsyh Cannot reproduce with develop branch.

$ php public/index.php /
site_url: http://localhost:8080/index.php/sample1
url_to: http://localhost:8080/index.php/sample-url-1
route_to: /sample-url-1
diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index fc4914a692..f6d3dae35e 100644
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -6,3 +6,5 @@ use CodeIgniter\Router\RouteCollection;
  * @var RouteCollection $routes
  */
 $routes->get('/', 'Home::index');
+
+$routes->get('sample-url-1', 'HomeController::index', ['as' => 'sample1']);
diff --git a/app/Config/Routing.php b/app/Config/Routing.php
index 8d3c773157..b9bf71d815 100644
--- a/app/Config/Routing.php
+++ b/app/Config/Routing.php
@@ -88,7 +88,7 @@ class Routing extends BaseRouting
      *
      * If FALSE, will stop searching and do NO automatic routing.
      */
-    public bool $autoRoute = false;
+    public bool $autoRoute = true;

     /**
      * If TRUE, will enable the use of the 'prioritize' option
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 5934333309..e2f0707959 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,8 @@ class Home extends BaseController
 {
     public function index(): string
     {
-        return view('welcome_message');
+        return 'site_url: ' . site_url('sample1') . PHP_EOL
+            . 'url_to: ' . url_to('sample1') . PHP_EOL
+            . 'route_to: ' . route_to('sample1') . PHP_EOL;
     }
 }

Maybe it's been fixed in develop branch? In master branch on CodeIgniter 4.4.1 it's still not working.

Here is a test set up:

Routes.php

$routes->cli('test', 'TestController::index');

TestController.php

<?php

namespace App\Controllers;

use CodeIgniter\Controller;

class TestController extends Controller {

    public function index() {

        echo site_url(); exit;

    }

}

Run now:

php index.php test

Output:

[Error]

Call to undefined method CodeIgniter\HTTP\URI::siteUrl()

at SYSTEMPATH/Helpers/url_helper.php:36

Backtrace:
  1    APPPATH/Controllers/TestController.php:11
       site_url()

  2    SYSTEMPATH/CodeIgniter.php:919
       App\Controllers\TestController()->index()

  3    SYSTEMPATH/CodeIgniter.php:494
       CodeIgniter\CodeIgniter()->runController(Object(App\Controllers\TestController))

  4    SYSTEMPATH/CodeIgniter.php:353
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

  5    FCPATH/index.php:79
       CodeIgniter\CodeIgniter()->run()
kenjis commented 1 year ago

@iamsyh Please check the develop branch.