CodeIgniter Forums
How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 (/showthread.php?tid=80162)



How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 - cloudytechi147 - 09-22-2021

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:
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 :

Code:
$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 the controller. Because I want to run a scheduled task cron job. And to prevent unauthorized users to run it from the browser. And spam it.

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

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

What is the correct way to call it?

Remember this is codeigniter 4 not codeigniter 3.

Thank You


RE: How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 - includebeer - 09-22-2021

Read this: http://codeigniter.com/user_guide/cli/cli.html
You should create a controller for your cron jobs and make your routes accessible only via CLI.
PHP Code:
$routes->cli('tools/message/(:segment)''Tools::message/$1'); 

In your controller, check is_cli() to make sure it's not accessed by a browser.
PHP Code:
if ( ! is_cli()) {
    // error




RE: How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 - iRedds - 09-24-2021

When you create an instance of a class
$object = new ClassName()
the class must be defined in this file or manually included or automatically included via the autoloader.

CI4 uses its own class autoloader and composer autoloader.
But since you do not have classes declared and the autoloader is not used, you get errors.


RE: How To Call A Model Or Class Using Custom Directory Path In Codeigniter 4 - paliz - 09-25-2021

define namesapce then call it class