-1

I have app using cakephp version 3. How to call a function from a custom php file.

Assume I have a custom php file test.php. And I want to call cakephp function from controller file UrlController.php.

The test.php

<?php
error_reporting(E_ALL);
include 'path/to/UrlController.php';
echo json_decode(GetUrl());

The UrlController.php

<?php

namespace App\Controller;

use App\Controller\URLController;
use Cake\Event\Event;
use Cake\I18n\Time;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\TableRegistry;

class LinksController extends URLController
{
    public function GetUrl()
    {
        $link = $this->Links->newEntity();
        $data = [];
        $link = $this->Links->patchEntity($link, $data);
        if ($this->Links->save($link)) {
            $content = [
                'status' => '200',
                'message' => 'success',
                'url' => 'https://google.com'
            ];
            $this->response->body(json_encode($content));
            return $this->response;
        }
    }
}

When tried to include app index.php and bootstrap.php it's still not working.

Edited test.php based on @Salines answer but still not working

<?php
namespace App\test;
error_reporting(E_ALL);

use App\Controller\URLController;

class Test extends URLController
{
   public function custom()
   {
     $this->getUrl(); // call function from UrlController 
   }
}

Error: "PHP Fatal error: Class 'App\Controller\URLController' not found in /../public_html/test/test.php on line 7"

My test file located at /absolute/path/public_html/test/test.php, what should I put in the namespace?

Drunken M
  • 2,013
  • 2
  • 11
  • 18
  • `require_once(ROOT . 'src' . DS . 'Controller' . DS . 'UrlController'); $LinksControllerObj = new LinksController();` – urfusion Aug 03 '19 at 09:53
  • Why would you do that in the first place? Your question kinda reads like an [**XY problem**](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – ndm Aug 03 '19 at 11:04
  • @ndm that's my actual problem, not XY problem. – Drunken M Aug 06 '19 at 10:39
  • Well, with all due respect, that's what most people say when they are confronted with the claim of a possible XY problem :) See, you want to directly invoke a controller from a non-CakePHP application PHP script, and you aren't able to do so, that's _a_ problem, the one that you are presenting here, but what I suspect, is that this is actually your _solution_ (an unfit one at that) to an underlying problem (whatever your non-CakePHP script is ment to do/solve) which you should elaborate on instead. – ndm Aug 06 '19 at 11:09
  • To make myself clear, trying to include a controller from outside of your CakePHP application is the wrong thing to do (even if you get it working), no matter what you're trying to solve by doing that. – ndm Aug 06 '19 at 11:11
  • Basically I want to combine 2 scripts. One is cakePHP, another is regular php. Why do you think it is a wrong thing? Any specific reason for that? – Drunken M Aug 06 '19 at 11:42
  • @ndm The non-CakePHP script ment to solve is to reduce http call overhead if calling the cakePHP function from url. since it was in same server, same site, I think of possibility to just include the function. – Drunken M Aug 06 '19 at 11:48
  • If you can't use an HTTP request, then that sounds like either the job for a [**shell/command**](https://book.cakephp.org/3.0/en/console-and-shells.html), or possibly the [**standalone ORM package**](https://github.com/cakephp/orm), making use of your application models if applicable. – ndm Aug 06 '19 at 12:45
  • I can use HTTP request, but the hits already thousands per minute and begin to overload the server. Do you think using shell will less overhead? – Drunken M Aug 06 '19 at 15:46

1 Answers1

0

In same way as you use Links Controller

test.php

<?php

namespace App\path_to_folder_where_is_your_test_php;

use App\Controller\URLController;

class Test extends UrlController
{
   pubic function custom()
   {
     $this->getUrl(); // call function from UrlController 
   }

Or in PHP without class

$newUrl = new \App\Controller\URLController();
$result = $newUrl->getUrl();

However, you do not comply with the MVC standard

Salines
  • 5,674
  • 3
  • 25
  • 50
  • Hi, I still can't make this works. Got this error "PHP Fatal error: Class 'App\Controller\URLController' not found in /../public_html/test/test.php on line 7". My test file located at /absolute/path/public_html/test/test.php, what should I put in the namespace? I have added the test.php code in the question. – Drunken M Aug 06 '19 at 10:38
  • Your test.php is not inside App folder? Then you must in composer add path to autoload from other destination. namespace in your /test/test.php file : namespace Test; – Salines Aug 06 '19 at 11:10
  • Can you tell me more how to do that? – Drunken M Aug 07 '19 at 06:13
  • yes, https://getcomposer.org/doc/04-schema.md#psr-0. But ideal place for your custom php script would be inside cakephp webroot folder – Salines Aug 07 '19 at 07:48