Include a file in laravel controller?

17,157

Solution 1

Thank you all for taking efforts in trying to solve my problem but none of the solution worked for me so, here is what I tried for my problem

Below is the structure of my php file that I wanted to includes/integrate

<?php

class Misc {

    const SUCCESS = 1;
    const FAILURE = 0;

    public static function get_hash ( $key )
    {
        ...
        ...
        ...
    }

    public static function show_reponse ( $result )
    {
        ...
    }

}

function check($keyhash) 
{
    ...
    ...
    ...
}

function function2() 
{
    ...
    ...
    ...
}

class Response {

    public function __construct ( $key )
    {
        ...
    }

    public function __destruct ()
    {
        unset( $this->key );
        unset( $this->params );
    }

    public function __set ( $key)
    {
        ...
    }

    public function __get ( $key )
    {
        return $this->params[$key];
    }

    private function check_now ()
    {
        ...
    }
}

The main problem I was facing is the class name Response which was conflicting with Laravel Response class so I just removed all classes from the file and moved to their individual files in a new folder in Laravel\App folder and added namespaces to all classes.

Then I moved all functions in a PHP file in laravel\App directory

and used classnames along with the namespace defined and since I moved all functions in a different PHP file I could easily call the functions

so here is my final folder structure of Laravel

Laravel

  -App
    -Console
    -Events
    -Exceptions
    -...
    -Libraries(Folder Containing Individual PHP files of classes from original file)
    -Providers
    -helpers.php(File containing all functions from original file)
    -User.php
  -bootstrap
  -...
  -...

Solution 2

If you have a custom file containing some classes/functions that need to be loaded for every request, you need to make sure it's added to the autoloader.

In your composer.json add the following in your autoload section:

"autoload": {
  "files": [
    "path/to/your/File.php"
  ]
}

This will make sure the file is loaded. Now what you need is a way to use those classes without conflicting with existing Laravel classes.

First, make sure you have a namespace declaration at the top of your included file - say namespace Your\Namespace. In order to avoid conflicts, you need to explicitly tell PHP which class you mean when you reference it in the code. You mentioned your file contains a Response class that also exists in Laravel. In order to be able to use both, you need to alias one of them:

use Illuminate\Http\Response as LaravelResponse;
use Your\Namespace\Response;

Now in your code you can refer to Laravel's Response class as LaravelResponse, and to your response by simply Response.

Location of the file is irrelevant, as long as it's in a folder accessible to Laravel and its patch is added to composer.json.

Keep in mind that storing multiple classes per file is discouraged as a bad practice. I strongly suggest that you split your fine into separate file per class + one additional file with global functions.

Share:
17,157
Akshay Khale
Author by

Akshay Khale

The quieter you become, the more you listen...

Updated on July 27, 2022

Comments

  • Akshay Khale
    Akshay Khale almost 2 years

    I am creating a project with some additional functionality provided in form of a .php file API which contains some functions and some classes(out of which some class names are conflicting with Laravel built in class names) so, my question how should I include this file in my Laravel Controller to call functions in the file which using the classes of the file without referring Laravel classes and with less or no modification in .php API file?

    Note* I am using Laravel-5.1