Welcome Guest, Not a member yet? Register   Sign In
Error using base_url inside CLI route
#1

After updating my project from 3.8.5 to 4.4.0 i've encountered an error with my CLI routes that send e-mails with a link to my website. To test I created a new project with composer and tried to echo the base url of my project. If someone could help me I'd be glad.

CLI output:

Code:
PS C:\projects\project-root> php public/index.php home

[AssertionError]

assert($currentURI instanceof SiteURI)

at SYSTEMPATH\Helpers\url_helper.php:52

Routes file:

PHP Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
* @var RouteCollection $routes
*/
$routes->cli('home''Home::index'); 

Home controller:

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function 
index()
    {
        echo 
base_url();
    }

Reply
#2

(This post was last modified: 09-05-2023, 01:38 PM by kenjis.)

Thank you for reporting.

Try this:

Code:
--- a/system/HTTP/CLIRequest.php
+++ b/system/HTTP/CLIRequest.php
@@ -71,6 +71,9 @@ class CLIRequest extends Request
         ignore_user_abort(true);

         $this->parseCommand();
+
+        // Set SiteURI for this request
+        $this->uri = new SiteURI($config, $this->getPath());
     }

     /**
Reply
#3

I sent a PR to fix this issue. https://github.com/codeigniter4/CodeIgniter4/pull/7901
Anyone can test and review.
Reply
#4

(09-05-2023, 01:36 PM)kenjis Wrote: Thank you for reporting.

Try this:

Code:
--- a/system/HTTP/CLIRequest.php
+++ b/system/HTTP/CLIRequest.php
@@ -71,6 +71,9 @@ class CLIRequest extends Request
        ignore_user_abort(true);

        $this->parseCommand();
+
+        // Set SiteURI for this request
+        $this->uri = new SiteURI($config, $this->getPath());
    }

    /**

It works, thanks! I'll wait the next update to use in production.
Reply
#5

I have the same problem, when executing a cron job by CLI, this method send also a email and get this error. The fixed is working for me too.
My error was detected when loading view file inside the controller for sending as email body:
Code:
$body = view('cm/mail/' . $action, $data);

I'm using CI 4.4.1 but need to patch file CLIRequest.php manually.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB