-1

I have to run a script every hour. So what I did in server is call myfile.php file every hour which runs my static function runT().

Now my problem is after this runT() function is executed i have to run a method test under a controller MyController.

myfile.php

include "Helper.php";

Helper::runT();

Helper.php

public static function runT() {
        ini_set('max_execution_time', 300);
        $conn = self::getDbConnection();

MyController.php

class MyController extends Controller
{
   public function test() {
Pritamkumar
  • 682
  • 1
  • 11
  • 30
S S
  • 1,443
  • 2
  • 12
  • 42

2 Answers2

2
app(MyController::class)->test();

This will resolve an instance of MyController, it will do the jobs like dependency injection for you, and then call the test method.

rubys
  • 583
  • 3
  • 11
  • so I just add app(MyController::class)->test() inside runT function ? – S S Jan 11 '19 at 04:57
  • Yes, of cause you should use fully namespace. – rubys Jan 11 '19 at 04:59
  • but i am getting following error: Fatal error: Call to undefined function app() in C:\xampp\htdocs\myapi\assets\auto\Helper.php – S S Jan 11 '19 at 05:07
  • hmm, if you haven't import composer autoload file, you will get this error. You can use import the autoload file like this, `require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__.'/../bootstrap/app.php';`, the path you should change to your locally relative path – rubys Jan 11 '19 at 05:09
  • I dont understand – S S Jan 11 '19 at 05:10
  • And beside, to auto run a job, you should use the the [artisan](https://laravel.com/docs/5.7/artisan) – rubys Jan 11 '19 at 05:13
0

to call controller method.use base_url.

 $.post('controller/method', $("#formid").serialize(), function (data) {
}
PHP Geek
  • 3,949
  • 1
  • 16
  • 32