Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bhavyasoft.com's avatar

How to call a Laravel Controller function outside Laravel Framework

Hi all,

I am calling Laravel Framework outside Laravel in a stand alone php file with the following code:

$path = "bootstrap/";

require $path . 'autoload.php'; $app = require_once $path . 'app.php';

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');

$response = $kernel->handle( $request = Illuminate\Http\Request::capture() );

print_r(Session::all());

The above code seams to working and displaing session Array.

I search but i did not find right way to call a function from "AuthController" class.

Any help will be appreciate very much.

Thanks!

0 likes
13 replies
somnathsah's avatar

Create a route for your controller method and call that from outside of laravel framework.

For example :

ExampleController:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller,
    Illuminate\Foundation\Auth\AuthenticatesUsers,
    Illuminate\Http\Request,
    \Illuminate\Support\Facades\Redirect,

class ExampleController extends Controller
{

      public function example()
      {
      }
}

Now create route in web.php as below

Route::get('/example', 'ExampleController@example');

Now you can call above controller from outside of your laravel framework by using {your base url for laravel app}/example.

bhavyasoft.com's avatar

Thanks for your reply but i dont want to create a new route.

I think you did not read my post properly, i want to call a function from a existing controller "AuthController" in a stand alone php file.

do you know how can i do that?

bhavyasoft.com's avatar

I am writng a migration script to import data to a laravel application, for that i have to import a user database from another application in that laravel application.

1 like
kire's avatar

Hi @bhavyasoft.com do you get to the solution about this problem i am very interested and have same problem and need solution also. Thanks to all in Advance.

1 like
iBims's avatar

Even though the thread is very old, I would like to have the solution shared here if anyone else ever comes across this thread as well.

Unfortunately, however, I am apparently not allowed to post the code since I am a new member. No idea for what reasons they restrict this.

Alexsighy's avatar

@ibims Step1. php artisan make:controller TestController and add a public function test() { return 1; } | Step2. Create a route for it (pretty much an API), and add Route::get('/api/test', [App\Http\Controllers\TestController::class, 'test']); | Step3. Access the function via url http://localhost/api/test | Also can someone help me figure out how to create a second raw when replying? I have to write everything in one long string.

iBims's avatar

This is not the solution.

The creator of the thread wants to access their methods/controller from outside of Laravel. Your solution only works by calling the URL, but the creator of this thread wants to access methods & classes of Laravel from another PHP script, which is outside of the Laravel root.

iBims's avatar

Here is the solution to access Laravel methods from outside Laravel:

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

# USE MY CUSTOM HELPER CLASS, FOR EXAMPLE
use App\Helper\SomeCustomFunctions;

# USE MY MyCustomController, FOR EXAMPLE
use App\Http\Controllers\MyCustomController;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists(__DIR__.'/PATH/TO/LARAVEL/storage/framework/maintenance.php')) {
    require __DIR__.'/PATH/TO/LARAVEL/storage/framework/maintenance.php';
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/PATH/TO/LARAVEL/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

$app = require_once __DIR__.'/PATH/TO/LARAVEL/bootstrap/app.php';

$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(
    $request = Request::capture()
));

$kernel->terminate($request, $response);

# CALL FUNCTION IN HELPER CLASS
SomeCustomFunctions::CustomFunctionInHelperClass();

# CALL FUNCTION IN MyCustomController CLASS
$res = MyCustomController::getData();
dd($res);
1 like
iBims's avatar

I came to this thread via google because I had the same problem.

Often the goal of such forums is that others with the same "problems" can find the solution.

This thread was never solved, but I think that I have now shared a solution and this could certainly help other users in the future.

So what exactly is your problem?

1 like

Please or to participate in this conversation.