Support the ongoing development of Laravel.io →
Configuration Blade

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 "myFunction.php" into app/myfolder/myFunction.php

  2. at app/start/global.php , within ClassLoader::addDirectories(array( , I add app_path().'/myfolder',

  3. And to call it within my blade view, I add the following codes :

    <?php $FmyFunction1 = new myFunction; $is_ok1=($FmyFunction1->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/myfolder/", to the autoload array and run composer dum-autoload , but it keeps give me error :

FatalErrorException in xxxx line xx: Class 'myFunction' not found
Last updated 3 years ago.
0

Have you named your class that ? Just give it a look.

Last updated 3 years ago.
0

Yes, I already do. Those class is working just fine in Laravel 4.

BTW nice Bioman avatar :)

Last updated 3 years ago.
0

Are you properly importing the class ? I assume you have put that class in global namespace.

L5 uses PSR-4 autoloading, try keeping your classes in namespaced.

... tX !

Last updated 3 years ago.
0

Here are my steps :

The content for app/library/myFunctions.php is below :

<?php namespace App\library {
	class myFunctions {
		public function is_ok() {
			return 'myFunction is OK';
		}
	}
}
?>

At resources/views/test.blade.php , I added :

<?php
  $FmyFunctions1 = new myFunctions;
  $is_ok = ($FmyFunctions1->is_ok());
?>

Then at composer.json within "autoload": I added :

    "files": [
        "app/library/myFunctions.php"
    ],

And within app\Http\Controllers\HomeController.php at public index() I add :

return view('test');

But it always give me error messages regarding 'myFunctions' as below :

Whoops, looks like something went wrong. 1/1 FatalErrorException in xxxx line 7: Class 'myFunctions' not found in xxxx line 7 at HandleExceptions->handleShutdown()

Last updated 3 years ago.
0
Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

sidhi sidhi Joined 22 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.