Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How import custom class in controller Laravel?

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
{
 //
}
like image 758
Dev Avatar asked Aug 08 '16 19:08

Dev


People also ask

What is :: class in laravel?

Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes. Copy Code namespace NS { class ClassName { } echo ClassName::class; }

What is controller class in laravel?

A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory.

Where are classes stored in laravel?

Introduction. Laravel's events provide a simple observer pattern implementation, allowing you to subscribe and listen for various events that occur within your application. Event classes are typically stored in the app/Events directory, while their listeners are stored in app/Listeners .


1 Answers

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());
?>
like image 158
cp50 Avatar answered Nov 12 '22 13:11

cp50