Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my own custom class in Laravel 5?

Tags:

class

laravel

Ok, in laravel 4, if I want to add my own custom class, eg : library\myFunction.php then I do the following steps :

  1. add "myFunctions.php" into app/library/myFunctiosn.php
  2. at app/start/global.php , within ClassLoader::addDirectories(array( , I add app_path().'/library',
  3. And to call it within my blade view, I add the following codes
<?php
  $FmyFunctions1 = new myFunctions;
  $is_ok1=($FmyFunctions1->is_ok());   
?>
  1. The contents of app/library/myFunctions.php is :
<?php namespace App\library {

    class myFunctions {
        public function is_ok() {
            return 'myFunction is OK';
        }
    }

}
?>

And it works.

But how to do so in Laravel 5 ???

PS : I read What are the best practices and best places for laravel 4 helpers or basic functions?

And tried to add "app/library/", to the autoload array and run composer dum-autoload , but it keeps give me error :

FatalErrorException in xxxx line xx: Class 'myFunctions' not found

I'm also already trying to use :

composer update
composer dump-autoload 
php artisan dump
php artisan clear-compiled
php artisan dump-autoload
php artisan optimize
php artisan route:clear
php artisan route:scan
php artisan route:list 

But still doesn't work...

like image 395
Galvion Avatar asked Nov 15 '14 04:11

Galvion


People also ask

Where are classes defined in Laravel?

The namespace is App\Classes so any class in the Classes directory is available under App\Classes namespace.

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 .

What is Laravel class?

Using the ::class keyword in Laravel Since PHP 5.5 the class keyword is used for class name resolution. This means it returns the fully qualified ClassName. This is extremely useful when you have namespaces, because the namespace is part of the returned name.


2 Answers

This should help you.

FYI: Basically, you could create another directory within app, and then namespace your files in there as appropriate:

app/CustomStuff/CustomDirectory/SomeClass.php.

Then, within your SomeClass.php, make sure you namespace it:

<?php 
namespace App\CustomStuff\CustomDirectory;

class Someclass {}

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

use App\CustomStuff\CustomDirectory\SomeClass;
like image 199
Captain Sparrow Avatar answered Oct 07 '22 09:10

Captain Sparrow


After some trial and error, I found the answer.

There is no need to modify Composer. Just modify the Blade into:

<?php
  $FmyFunctions1 = new \App\library\myFunctions;
  $is_ok = ($FmyFunctions1->is_ok());
?>
like image 27
Galvion Avatar answered Oct 07 '22 08:10

Galvion