Reputation: 1064
I have class in libraries/myclass.php
and a function named myFunction
. Also I try to call that function in my controller with the following code
$myclasobj=new libraries\Myclass();
$returnvalue=$myclasobj->myFunction($para);
results that class not found. I dont want it as a helper class and auto load this class. I just want to use as a simple class.I am using laravel 4.How can I obtain this?
Update
Thanks all of you for your great help.
As per @carousel,@Ohgodwhy I make it by namespacing (Actually before I don't know about it).
I make a directory under libraries named mylib
and move my class (myclass.php
) to it and after that I add namespace mylib;
to the top of myclass.php
.
And after that I add use mylib\Myclass;
to my controller.
Finally I add these lines to composer.json
,
"psr-0": {
"mylib": "app/libraries"
},
After these things my class is working.Thanks to all for helping me
Upvotes: 0
Views: 1714
Reputation: 860
Assuming that the file hasn't been loaded yet, the proper way to add your libraries/ directory to the classloader would be to modify your composer.json to add the directory to the autoloader.
autoload: {
classmap: [
"...",
"libraries/"
]
}
After that, you'll need to rebuild the autoloader, so in the home directory, go ahead and run this in your terminal:
$ php artisan dump-autoload -o
Also ensure that your class is actually contained inside a namespace, because using
new libraries\myClass();
Does not load "myClass" from the "libraries" folder. It would be helpful if you could add it to the original question.
Upvotes: 2
Reputation: 3455
Every new class, in order to be used in Laravel, has to be linked to Application. It can be done in more then one way:
Through class map
With namespacing
By its path
In that sense there is no simple classes. All classes can become a part of Laravel Application flow, if they are referenced correctly with composer.
UPDATE TO MY ANSWER:
Here is a link to best resource that explaines facades.
Upvotes: 2
Reputation: 2783
Try doing this, it should solve your problem:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/classes', //we added this
));
Upvotes: 1