6

I have created a new directory Library in root of Laravel.

Inside I put the file with class:

class My { // }

So, in controller Laravel I try to get access to this class:

App\Library\My

But Laravel does not determine this path.

This is my code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use View;

use App\Library\My;

class HomeController extends Controller
{
 //
}
2
  • Did you use psr-4 if not run composer dump-autoload ! Commented Aug 8, 2016 at 19:33
  • It did not help: composer dump-autoload Commented Aug 8, 2016 at 19:42

4 Answers 4

6

A complete and functional example based on the posts here:

1 - Folder and file - Create a folder under app/, in this example we will create a folder called Library.

We will also inside the folder create a file with the name of your class, here we will create a class called My.

So we will have app/Library/My.php

2 - Class and method - Now, for testing, inside the class create a static method called myMethod

<?php

namespace App\Library;

class My
{
    public static function myMethod() 
    {
        return 'it\'s work!';
    }
}

3 - Controller - Now at the beginning of the Controller, we will declare the namespace of your Class with use:

<?php

namespace App\Http\Controllers;
use App\Library\My;

//rest of the controller code

Finally, to create an instance of the My class, in Controller, a new statement must be used:

//rest of the controller code

public function index()
{ 
    $whatever = new My;
    return $whatever::myMethod();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I like how elaborate your answer is.
4

As above, make sure it is placed in the App directory and make sure it is properly namespaced e.g.

<?php
  $fOne = new \App\library\functions;
  $isOk = ($fOne->isOk());
?>

Comments

2

You should create Library folder inside app folder

namespace App\Library\My

app folder is alrdy used psr-4

In your controller

use App\Library\My as My

It's work for me. Hope this answer is helpful

3 Comments

If when I want to use some namespaces?
If you want use another namespace without app like Library\My look at file composer.json at autoload > psr-4. Insert new your custom autoload and run composer dump-autoload in terminal
When I mouse over this ` use App\Library\My;` I get message: undefined namespace
2

You have to properly namespace your every class.

So you can import your class with use keyword, like so

use App\Library\My;

....

$my = new My();

Or if you've conflicting class name then you can use as keyword to alias the classname while importing

use App\Library\My as MySecond;

....

$my = new MySecond();

And if you want to directly access your class within the method then you can access it like so.

$my = new \App\Library\My();

Note: The leading \ means App was declared in the global scope.

4 Comments

use App\Library\My; - undefined namespace
@Dev show me the code of your class and where you are trying to access the class?
May be I should to use provider?
Don´t mix it with java import. Unfortunetely php´s use does not import anything. It just refers to some namespace or class-name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.