3

I want to include an external PHP file into my service provider, that file is in a different folder. Like my file is in folder1 and this folder is at same level as laravel is.

   C:\xampp\htdocs\registration\php\file.php //this is file
   C:\xampp\htdocs\_problem_sharing\app\AppServiceProvider

This is how I am trying right now

include_once "/../../../registration/php/user_info.php";
6
  • Going two level back: include_once "../../registration/php/user_info.php"; should be enought. Also, don't prepend the relative path with /. Commented Dec 29, 2015 at 21:13
  • its still looking the file under Providers directory @Bogdan Commented Dec 29, 2015 at 22:03
  • What's the error that you're getting? Commented Dec 29, 2015 at 22:28
  • FatalErrorException in AppServiceProvider.php line 14: Class 'App\Providers\UserInfo' not found where UserInfo is my class name in the file that I am trying to include. @Bogdan this happens when I try to make class object $this->userInfo = new UserInfo(); Commented Dec 29, 2015 at 22:31
  • Since the provideo is in it's own namespace App\Providers, you should either put this at the top of your provideor class file use UserInfo, or whenever using the class inside the code prepend it with a backslash which puts it in the global namespace (for ex: new \UserInfo()). If the problem persists, please post the contents of your provider. Commented Dec 29, 2015 at 22:39

1 Answer 1

1

Is really simple to do this. Because everything in Laravel 5 is autoloaded using PSR-4, within the app/ directory. So, for example, if this file you want to include have a class.

You need to create the directory, e. g.: app/CustomStuff/CustomDirectory/ Into this directory, create the file: app/CustomStuff/CustomDirectory/SomeClass.php

Into the SomeClass.php file, you just need:

<?php 
namespace App\CustomStuff\CustomDirectory;

class Someclass {}

Now, you can access this class using the namespace within your classes:

use App\CustomStuff\CustomDirectory\SomeClass;
Sign up to request clarification or add additional context in comments.

4 Comments

i do not want to copy that file into laravel's directory
I think that's not possible, you have to copy the file into Laravel
@VitorLuis Of course it's possible, you can load any file from anywhere in the filesystem as long as you have permission to read it, you just need to use include or require, or if you want to you autoload it using Composer, you need to configure composer.json.
You could symlink the file into laravel, this will ensure the file is tracked by git as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.