I have a php library class file called, CsvClass.php. I place this file in the location, app/Libraries/CsvClass.php. I have seen Some url where this kind of problem have the solution. So as per the direction I added namespace App\Libraries; and use App\Libraries\CsvClass; in the controller. But for this I have this error at the time of load the controller. FatalErrorException in ImportController.php line 17:Class 'App\Libraries\Controller' not found.
So I comment app/Libraries/CsvClass.php and run so the controller is loading. At the top portion of the controller is now look like this,
namespace App\Http\Controllers;
//namespace App\Libraries;
use App\Libraries\CsvClass;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Http\RedirectResponse;
use Clusterpoint\Client;
use DateTime;
use Session;
use Excel;
But when I try to use the class and there is an error of,
FatalErrorException in ImportController.php line 120: Class 'App\Http\Controllers\parseCSV' not found
I try trying to use this class,
$csv = new parseCSV();
$csv->auto($path);
$full_data=$csv->data;
Here $path contain the path of the csv file. This function is working in a separate php file. But not in Laravel. I am using version 5.3. And follow the rules of how to import the external files in laravel. But not understand why the error is coming. Please help me.
I am using a CSV library to read csv file. The Library is here.
The best way to use classes without namespaces and different file name is by using composer autoloading.
1) Register your class in classmap.
"autoload": {
"classmap": [
"database",
"app/Libraries/CsvClass.php"
],
"psr-4": {
"App\\": "app/"
}
},
2) Run composer dump-autoload
Now you can access your class anywhere without providing the full path.
$csv = new parseCSV();
$csv->auto($path);
$full_data=$csv->data;
Related
I just started using CakePHP 3. I'm trying to get up and running but doing the simplest of things is proving to be a headache.
I have a class, MySimpleClass, in src/App/MySimpleClass.php
<?php
namespace MyApp\MyNamespace;
class MySimpleClass {
public function aSimpleFunction() {
return 1;
}
}
And in my controller:
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use MyApp\MyNamespace\MySimpleClass;
class MyFirstController extends Controller {
public function display() {
$mySimpleClass = new MySimpleClass();
echo $mySimpleClass->aSimpleFunction();
}
}
But this always gives me:
Error in: ROOT/src/Controller/TestController.php, line 10 Class 'MyApp\MyNamespace\MySimpleClass' not found
I use bin/cake server to run the HTTP server
I added App::className('MyApp\MyNamespace\MySimpleClass'); to bootstrap.php to see if that'd make a difference but it doesn't.
I've run composer dump-autoload on several occasions.
I tried putting MySimpleClass into global namespace but it still gave me the error.
PHPStorm isn't giving me any syntax or naming errors.
You should place your controllers in:
src/Controller
and use:
namespace App\Controller;
Nevermind, I finally found the solution...
I just had to add src to the classmap in composer.json:
"autoload": {
"classmap": [
"src"
],
...,
}
Edit:
Instead of abusing classmap, I just moved my class files to a directory outside of src
If your class is in the file src/App/MySimpleClass.php, the namespace has to be App\App. The first App refers to the root namespace of every CakePHP 3.x application, the second App refers to the subdirectory within the src directory you put your class file into.
If the namespace of the class should be App\MyNamespace, your classfile has to be located in src/MyNamespace.
Also: according to the error message you quoted, your MyFirstController is in a file called TestController.php. Instead, it should be MyFirstController.php. I recommend giving https://book.cakephp.org/3.0/en/intro/conventions.html#file-and-class-name-conventions a read regarding this topic.
I am just start working with Laravel, want to create a custom class and want to call this class in every controller. For this, I create a Customer class in app/Library/ folder.
When I tried to autoload this library via composer, json it's giving an error:
Could not scan for classes inside "App/Library/Customer" which does not appear to be a file nor a folder.
How can we use autoload class in controllers?
Customer.php
<?php
namespace App\Library;
use App\Model\User;
class Customer
{
public function login($user_name,$password){
$data = User::where('email', $user_name)
->where('password', $password)
->first();
return $data->id';
}
}
Autoload section of Composer.json
{
"autoload": {
"classmap": [
"database",
"app/Library/Customer"
],
"psr-4": {
"App\\": "app/"
},
"files" : [
"app/Helper/helper.php"
]
}
}
I think you're not understanding what the composer autoload is there for. You use this to include libraries and their dependencies, not really for classes you've created in your app.
What you're better off doing is when you create the controller add in the class you want to use eg:
<?php
use App\Library\Customer;
You will need to put this in every controller.
You should remove it from the classmap group and just add the proper namespace and class. You can see all the psr-4 standards here: http://www.php-fig.org/psr/psr-4/
Lets say you have a folder structure like this:
app
-> Library
-> Customer.php // namespace App\Library; class Customer{}
-> Model
-> User.php // namespace App\Model; class User{}
And all the files should autoload as long as you use the proper namespace and class names.
By the way you should use the Auth facade instead: https://laravel.com/docs/5.4/authentication
There is no need to classmap as already psr-4 autoloading is in place. You've to understand how it works. then you can simply import your classes using use keyword, like this
<?php
use App\Library\Customer;
For more information read PSR-4: Autoloader and take this Tutorial
I am trying to change an autoloading system that I've written before.
I'm using composer and at the moment I'm autoloading just one library with class map.
"autoload": {
"classmap": ["libs/"]
}
I want to add a psr-4 loader for the rest of the files and to be able to call the files under libs without namespaces and without "use" them' kind of like aliases in laravel. This is what I'm trying to do:
"autoload": {
"classmap": ["libs/"],
"psr-4": {
"App\\": ""
}
}
So eventually if in "libs" I have the Session class I'm calling it as:
Session::get('anything')
but now after trying to add the psr-4 and calling it from within a namespaced class
namespace App\models;
Class User{
function get(){
return Session::get('anything');
}
}
It won't work anymore because it looks for session within the user's namespace.
I know there are many frameworks which implements it out of the box with aliases.., it's just that this project is kinda old and I'm trying to organize it a bit and get rid of all the requires anywhere - at the moment each model has to be required.
I want to add a psr-4 loader for the rest of the files and to be able to call the files under libs without namespaces and without "use" them' kind of like aliases in laravel.
You can't use the classes without either adding use or adding the fully qualified namespace path starting with the backslash \. This has nothing to do with the way you load these classes, but is a basic requirement of PHP itself - so there is no way around it no matter how you'd like to design your autoloading.
As was commented, adding a backslash works, but this is the required minimum:
namespace App\models;
Class User{
function get(){
return \Session::get('anything');
}
}
I've ended up writing another class that aliasing any classses that i want so i will be able to call them out of the box.
you can see it here:
https://github.com/shahafan/SAmvc-App/blob/master/Config/Aliases.php
basically i'm just using the php class_alias function so i can load all the classes that i want before using them.
i think laravel does it the same way.
I'm trying to use PHPMailer through composer.
I'm using namespace and PSR-4 autoloading for my app
My file organisation is like this
-bin
-controllers
- Controller.php (namespace bin\controllers)
-vendor
-phpmailer
-phpmailer
-src
PHPMailer.php (namespace HPMailer\PHPMailer)
In composer.json, I wrote this
"autoload": {
"psr-4": {"bin\\vendor\\phpmailer\\": "bin/vendor/phpmailer/phpmailer/src/PHPMailer.php"}
}
But when I use
<?php
namespace bin\controllers;
use bin\vendor\phpmailer\PHPMailer;
abstract class Controller {
public function __construct()
{
$mail = new PHPMailer();
}
}
I got a fatal error :
require_once(): Failed opening required '/Users/thomas/Library/Mobile
Documents/com~apple~CloudDocs/saveProject/bin/../bin/vendor/phpmailer/PHPMailer.php'
How I could configure the namespace to use the class correctly?
Thanks a lot in advance.
You're mixing up paths and namespaces; for the most part they are independent, but PSR-4 and friends help to map one to the other.
PHPMailer only supports namespaces as of version 6.0, which is not yet released, though you can of course use a pre-release version.
use bin\vendor\phpmailer\PHPMailer;
Should be:
use PHPMailer\PHPMailer\PHPMailer;
The use keyword is used to import an entity from another namespace into the current one - note that this is conceptually different from including a file with include / require.
It looks like you're mixing up namespaces in your own app too - typically you'd use a namespace that's based on a <vendorname>\<projectname> pattern, so your example controller might be :
namespace MyCompany\MyProject;
class MyMailController...
Or you could make use of multiple namespaces within your own project like this:
namespace MyCompany\MyProject\Controllers;
class Mail...
which would define the MyCompany\MyProject\Controllers\Mail class.
So I've been trying to create a custom class file for all my custom classes and append them in there, I'm using namespaces in order to accomplish using them in my controller however the controller is not recognizing the class
Ok:
Here is my custom class file placed in my project as app/Library/robloxClasses.php
namespace App\Library;
class RobloxMaths{
public function sortArrayOfArray(&$array, $subfield)
{
$sortarray = array();
foreach ($array as $key => $row)
{
$sortarray[$key] = $row[$subfield];
}
array_multisort($sortarray, SORT_DESC, $array);
}
}
Library is a folder i made to store all my custom class files in there
Here is how my controller ApiController uses the file using it's namespace
<?php
namespace App\Http\Controllers;
use App\Library\RobloxMaths;
use Illuminate\Http\Request;
class ApiController extends Controller
{
protected $RobloxClass;
public function __construct(){
$this->$RobloxClass= new RobloxMaths;
}
}
?>
The problem is, it keeps returning:
Class 'App\Library\RobloxMaths' not found
And I honestly do not know what's wrong, please help!
NOTE: I have tried composer update, and it fixes it temporarily but after a day or two or after me installing new dependencies it notifies me that the Class 'App\Library\RobloxMaths' not found thing happened
help please
Ensure that you have correctly setup the psr-4 autoload root in your composer.json file and that your file/directory names match perfectly including capitalisation, then run composer dump-autoload.
Example:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
This will assign the root namespace App to the app folder.
If you are using an IDE, there is a large chance that your use and namespace statements where auto-generated with the lowercase app to match the directory name. As all the names tie up correctly in the IDE there are no errors, but when it comes to running the PHP script the autoloader is not doing require|require_once on your files and therefore your classes are not available.
Option 1 (Do this)
Ensure that your namespaces have consistent capitalisation that matches the composer.json entry.
Option 2 (Written for completion)
Ensure that you add a new entry for this root namespace to your composer.json file. For the example above this would be:
"autoload": {
"psr-4": {
"App\\": "app/",
"app\\": "app/"
}
}
It's an old question, but I had the same problem with laravel 9,
The problem is that I've used php short tag <? instead of <?php at the custom class file.
it's silly but this was the solution for me.