0

Guy I need support here For codeigniter 4. Please Help.

I have a folder and file in a root directory with full path :

TestingWebsite\app\CronJob\ExportAlert.php

The File Is Not In Controller.

Inside the ExportAlert.php the code :

<?php

            
date_default_timezone_set("Asia/Jakarta");
$current_time = date("h:i a");
$begin = "9:00 am";
$end   = "3:30 pm";

$now = DateTime::createFromFormat('H:i a', $current_time);
$timebegin = DateTime::createFromFormat('H:i a', $begin);
$timeend = DateTime::createFromFormat('H:i a', $end);

if (!($now > $timebegin && $now < $timeend)) {
return false;
} 


$Utility = new \App\Models\Dennis_utility_model(); // Error In Here Class Not Found
$Setting = new \App\Models\Dennis_setting_model();  // Error In Here Class Not Found
$Medoo = new \App\Models\Dennis_medoo_model(); // Error In Here Class Not Found
$Telegram = new \App\Models\Dennis_telegram_model(); // Error In Here Class Not Found


$ExeFile = AmiBroker_Folder.JScript_Main_Folder.Export_Alert;
$Url = $Setting->FPathSetting()['CROSSSERVER']['CURLEXE'];
$Utility->CurlExe($Url, 1, 1, $Setting->FUseEngine64(), $ExeFile);

$myconfig = new \Config\MyConfig();

include 'include/Alert_1.php';         



?>

I want to call the model :

$Utility = new \App\Models\Dennis_utility_model(); // Error In Here Class Not Found
$Setting = new \App\Models\Dennis_setting_model();  // Error In Here Class Not Found
$Medoo = new \App\Models\Dennis_medoo_model(); // Error In Here Class Not Found
$Telegram = new \App\Models\Dennis_telegram_model(); // Error In Here Class Not Found

Inside the ExportAlert.php

But I got Error : Class Not Found For Every Model I called.

I have a reason why I don't want to put it inside controller. Because I want to run a schedule task cron job. And to prevent unauthorize user to run it from browser. And spam it.

How I can call a model or class from outside controller use custom directory ?

In the file ExportAlert.php I didn't use the class. And just use a native php with model. Is it possible ?

What is the correct way to call it ?

Remember this is codeigniter 4 not codeigniter 3.

Thank You

Dennis Liu
  • 2,268
  • 3
  • 21
  • 43

1 Answers1

0

One possible issue I can see is when you use path "\App\Models\Dennis_utility_model()" directly it will not know where that particular path is since Autoloader is not used to load the modules and this is run as vanilla php script.

Besides this, you can certainly use Controllers as queues and not allow users to execute from the browser.

There are two ways to build queue:

  1. Using Controller which restricts the access to the CLI.
  2. Using Command CLI.

By using Second Way i.e Command CLI. You can create command (similiar in the way how spark commands work) and put that command in the CRONJOB to run at particular time. Also, since these are stored in Command folder they are not available to your users as route.

Refer: https://codeigniter4.github.io/userguide/cli/cli_commands.html?highlight=command%20line

This is in my opinion a correct way, however, I have always been using the first way so I cannot provide my own example for this.

Now lets check First Method i.e Via Controller. The way I do so is to define a Controller with longer and unintutive name as a FunctionalityProcessingQueue and since these are in another module folder, they are not available as route to the users.

Even if they are available to the user as route, you can easily check for it. by checking for CLI.

You can refer to the following URL : What is the canonical way to determine commandline vs. http execution of a PHP script?

It shows you on how to do so.

Now as far as the implementation is concerned:

You can set something like FunctionalityProcessingQueue class in your app folder.

So, it will look like app/Controllers/FunctionalityProcessingQueue.php

and your code will be similiar to other Contollers.

One thing you can add is set_time_limit(0) to increase the script time limit to timeout.

Example:

<?php

...
set_time_limit(0);
class FunctionalityProcessingQueue extends Controller {}

Then create a method in your Controller.

such as following :

...
 public function process(...$args)
    {}
...

This will pass parameters. You can use this args value for some condition check as well. Example token, to verify you are the one hitting it or other useful data.

From the CLI: Goto the route of your project and use following command :

php public/index.php FunctionalityProcessingQueue process some_parameter

Refer : https://codeigniter4.github.io/userguide/cli/cli.html https://codeigniter4.github.io/userguide/cli/cli_library.html

Dhaval Chheda
  • 844
  • 1
  • 8
  • 15
  • can we specified the part "\App\Models\Dennis_utility_model()" in codeigniter 4 ? How to to it ? – Dennis Liu Sep 15 '21 at 07:17
  • It will still require you to go through above ways. You can try by simply including the Autoloader in your orginal script and try it out. Following link: https://stackoverflow.com/questions/32642983/how-do-i-include-a-composer-package-into-plain-php/32645811 , This shows you how to include packages using autoloader. You will need to map the path to your needed path. Reference : https://codeigniter4.github.io/userguide/concepts/autoloader.html?highlight=autoload – Dhaval Chheda Sep 15 '21 at 08:24
  • I just think I just need to run it from CLI. Do you know how to run controller from CLI ? Aslo this documentation : https://codeigniter4.github.io/userguide/cli/cli.html but I cannot make it work. How to run it from CLI ? when it work I will try to execute command through cronjob – Dennis Liu Sep 15 '21 at 09:52
  • I have mentioned it in my answer to run it you should first test it from terminal using following command in the root of your project : php public/index.php ControllerName methodName. If you are going to run via cronjob you need to use full path such as path_to_root_folder/public/index.php – Dhaval Chheda Sep 15 '21 at 12:59