Skip to content

Bug: Cant use route_to function in cli request #6377

Closed
@dobaniashish

Description

@dobaniashish

PHP Version

7.4

CodeIgniter4 Version

4.2.4

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

No response

What happened?

Cant use route_to function in cli request because localization is not supported in CLI.

Locale detection only works for web-based requests that use the IncomingRequest class. Command-line requests will not have these features.

Here's the error trace:

CRITICAL - 2022-08-14 09:36:22 --> Call to undefined method CodeIgniter\HTTP\CLIRequest::getLocale()
in SYSTEMPATH/Router/RouteCollection.php on line 1061.
 1 SYSTEMPATH/Router/RouteCollection.php(1004): CodeIgniter\Router\RouteCollection->localizeRoute()
 2 SYSTEMPATH/Common.php(915): CodeIgniter\Router\RouteCollection->reverseRoute()
 3 APPPATH/Helpers/common_helper.php(154): route_to()
 4 APPPATH/Controllers/Process.php(531): get_image()
 5 APPPATH/Controllers/Process.php(1099): App\Controllers\Process->save()
 6 SYSTEMPATH/CodeIgniter.php(898): App\Controllers\Process->cronTask()
 7 SYSTEMPATH/CodeIgniter.php(457): CodeIgniter\CodeIgniter->runController()
 8 SYSTEMPATH/CodeIgniter.php(340): CodeIgniter\CodeIgniter->handleRequest()
 9 FCPATH/index.php(67): CodeIgniter\CodeIgniter->run()

I am not using localization in my script. I am using route_to() to get path for the image asset so that the required image can be generated in the background. This helps to follow the DRY principle.

Steps to Reproduce

Call a route_to() function in CLI request.

Expected Output

route_to() should return the path that was requested.

Anything else?

Update with more details:

I have this function in helper it helps to return either an absolute path to an image or its url:

function get_image($post, $type = 'url')
{
	$year = date('Y', strtotime($post['date_added']));
	$month = date('m', strtotime($post['date_added']));

	$path = ltrim(route_to('image', $year, $month, $post['slug']), '/');

	if ($type === 'path') {
		return FCPATH . $path;
	}

	return base_url($path);
}

The image route:

$routes->get('images/(:num)/(:num)/(:segment).png', 'Post::image/$1/$2/$3', ['as' => 'image']);

The image is displayed to the front like this:

<img src="<?php echo get_image($site); ?>" width="16" height="16">

The Post::image() controller function logs if any request is received so that I know that some image was not generated automatically.

In the background a cron creates the image to the path returned by get_image() for specific post.

So when the cron is run in cli, the route_to() function gives the above error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions