skip to Main Content

I have two virtualhost on a server. Each virtualhost has a laravel project installed (Project 1 and Project 2)

In Project 1 I have configured an API.

Can I call API methods from Project 2 without having to make an API call (guzzle or http request)? Directly accessing the controller of project 1?

thxs

I can’t find how to call a controller from another Laravel project

2

Answers


  1. Actually, there’s no straightforward way to handle this because the code of each project lives in its own namespace, so you can’t directly access the other controller.

    However, you can try communication between the two projects through IPC (Inter-Process Communication)

    In computer science, inter-process communication or interprocess communication (IPC) refers specifically to the mechanisms an operating system provides to allow the processes to manage shared data. (source https://en.wikipedia.org/wiki/Inter-process_communication)

    If you’re using services classes to handle business logic instead of writing all the code inside the controller, then you can proceed to create a custom artisan command that will invoke the adjacent method inside the service class, and in case you need to pass parameters then you can do this as well passing them one-by-one or serializing them into a JSON string that gets sent as a single parameter, and the receiving command will parse it and extract the data.

    For example, let’s say that the first project has a command that extracts username from a string as follows:

    <?php
    
    namespace AppConsoleCommands;
    
    use IlluminateConsoleCommand;
    
    class ExtractUsername extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'app:extract-username {user}';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Extracts username from a JSON string';
    
        /**
         * Execute the console command.
         */
        public function handle(): void
        {
            $user = $this->argument("user");
            $info = json_decode($user);
            $this->line($info->username);
        }
    }
    

    Now, in order to call it from within the second project, you can use the Process facade that’s available under the namespace IlluminateSupportFacadesProcess, it returns an instance of IlluminateProcessProcessResult which you can call the output() method to get the output of the process, here’s an example:

    <?php
    
    use IlluminateSupportFacadesProcess;
    
    $processResult = Process::run("php /absolute/path/to/second/project/artisan app:extract-username '{"username": "Someone" }'");
    
    $output = $processResult->output(); // "Someone"
    

    Please notice that the run() method is synchronous (i.e. it blocks execution of the script until the forked process terminates)

    For asynchronous behavior, consult the start() method in Laravel documentation about Processes management

    I hope I’ve made the answer clear and understandable

    Login or Signup to reply.
  2. No, you cannot call API methods from Project 2 without making a call using Guzzle or HTTP requests.

    If you try to call the methods directly from Project 2, without making an API call, you will run into various issues, such as security vulnerabilities.

    It’s not recommended but, if you want such access, you can do it like this:

    include_once('/path/to/Project1/app/Http/Controllers/Controller.php');
    
    $controller = new Controller();
    $controller->method();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search