I have an controller in default path of Laravel project
/app/Http/Controllers/PaymentController.php
In the same path there is an directory:
/app/Http/Controllers/merchant/
with files inside.
I tried to include one file from this directory in controller like:
require_once './merchant/private/filter/filter.php';
But it does not work. What do I do wrong, or Laravel does not allow to do this?
I get this error:
main(): Failed opening required './merchant/private/filter/filter.php'
(include_path='.:/usr/share/pear:/usr/share/php')
Don't use require_*. Laravel uses autoloading from composer. Therefore your class must have a proper namespace definition like
namespace App\Http\Controller\Merchant\Private\Filter;
which you use via a use statement
use App\Http\Controller\Merchant\Private\Filter\Filter;
Classes should be UpperCamelCase -> Filter.php instead of filter.php
Related
In my default symfony4 structure I want to add lib folder, where I have additional classes. So something like this:
-bin
-config
-lib
- Importer.php
...(other files with classes)
-public
-src
- Controller
- TestController.php
- Entity
- Form
...
...
But I cannot figure out how to later use my files (i.e.: Importer.php).
Let's say Importer.php has a single class Importer() inside. If I try to use it from TestController.php I get:
Attempted to load class "Importer" from namespace "lib". Did you
forget a "use" statement for another namespace?
TestController.php has
use Importer;
specified on top (autodetected by PhpStorm). I also tried adding namespace in my Importer.php file, for example:
namespace lib;
and then in TestController:
use lib\Importer;
But it produces the same result.
Lastly after reading about services, I tried adding the file to config/services.yaml
lib\:
resource: '../lib/Importer.php'
Which gives the same result...
What to do, how to live?
First of all read about php namespaces.
Next read about the psr-4 standart.
Select a prefix for your folder, let's say Lib. Make sure that all files in the lib folder has a properly namespace. E.g. Importer class must be stored in the lib\Importer.php and must have the namespace Lib;, Items\Item class must be stored in the lib\Items\Item.php and must have the namespace Lib\Items\Item; and so on.
Your files are ready. Just need to inform Symfony about them.
Symfony uses composer's autoloader, so check composer's autoload section. Than add new folder for autoloading in composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"Lib\\": "lib/"
}
},
It says that all classes in lib folder have their own separate files and Lib prefix in their namespace and other part of namespace is similar to directories structure.
Next you need to clear autoloader's cache. Run in console:
composer dump-autoload
And finally you can use your class:
use Lib\Importer;
$importer = new Importer;
Also you can add your files to autowire.
I'm attempting to use a class from a repository I installed from packagist with composer. The PHP file I'm writing is under my Project folder, along with the repository which is in Project>vendor.
include "Project/vendor/autoload.php";
include "Project/vendor/smartcat/smartcat-api/src/SmartCAT/API/SmartCAT.php";
use SmartCAT\API\SmartCAT;
use SmartCAT\API\Model\CreateProjectWithFilesModel;
This is at the top of my file and it is how I'm retrieving the smartcat class from the repository. When I try to use the class I wrote $sc= new SmartCAT($login, $password);. This causes the error: PHP Fatal error: Uncaught Error: Class 'SmartCAT\API\SmartCAT' not found in C:\Users\...\Project\createProject.php:20. When I run get_included_files() the necessary files are included. I'm not sure why I'm unable to use the class.
Did you try using the vendor/autoload.php path in the include directive?
If your createProject.php file and vendor folders are located in the same folder, you need to use include "vendor/autoload.php";
I can access the list of files in a directory in Laravel using this:
use File;
....
$files = File::files($path);
However, I get this error in lumen:
Class 'File' not found
Any idea how I can access list of files in a folder in lumen.
File is only avaiable in Laravel by default. Although you can still used it on Lumen Framework by doing the following.
Enable the facade in boostrap/app.php by uncommenting the following code.
$app->withFacades();
After that, you will be able to access the File class in any of your controller by adding it as:
use Illuminate\Http\File;
or you can use the Facade Class.
use Illuminate\Support\Facades\File;
Please do a composer dump-autoload to update autoload.
I am trying to learn Symfony 2. I'm trying to follow the example in the Symfony book (Page 17). I have downloaded and unpacked Symfony in my working directory.
The absolute path to the file I'm trying to use is in: symfony\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Request.php
However following the books example closely, I just put:
use Symfony\Component\HttpFoundation\Request
Either way, both these approaches did not work for me.
Using the books approach I'm getting the following error:
Fatal error: Class 'Symfony\Component\HttpFoundation\Request' not found in C:/...
Using my approach with the longer path, I'm still getting this error, when I use include to navigate to the long path, the file is found but I'm getting some other error (just wanted to verify I'm able to get to the file, which I am).
I appreciate any advice in overcoming this problem. Many thanks in advance!
in PHP use statement requires Fully Qualified Class Name, not directory. So use Symfony\Component\HttpFoundation\Request is just fine.
If you decided to try these "examples" you will have to require autoload.php file first. It is located in your vendors directory.
So if you make file named "myfile" in symfony web folder it should start like this:
<?php
require '../vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
Run - > composer update
Run the Path -> ./vendor/bin/upgrade-carbon
Then Will fixed this issue in laravel
When trying to implement this framework I ran into a issue with the directory. When I attemt to use this Zend i end up with
Interface 'Zend\Mail\Storage\Folder\FolderInterface' not found in /home/content/54/9595754/html/zend/library/Zend/Mail/Storage/Imap.php
In the index.php file where I try to display the emails. I use an:
set_include_path
include require_once('Imap.php');
The contents of Imap.php looks like:
namespace Zend\Mail\Storage;
use Zend\Mail;
use Zend\Mail\Protocol;
class Imap extends AbstractStorage implements Folder\FolderInterface, Writable\WritableInterface
So what would i need to add to the Imap.php so that it dosent look inside this directory for the file.
I know understand that I need to try to implement a file in order for the framework to understand the directory they are in however when i try to implement an autoloader I get an error see example below.
require_once '../../../library/Zend/Loader/Autoloader.php';
Yields
require_once(Zend/Loader.php) [function.require-once]: failed to open stream:
No such file or directory in /home/content/54/9595754/html/zend/library/Zend/
Loader/Autoloader.php
Zend framework 2 and most other modern frameworks rely on autoloading for class loading.
You have only required the Imap class but not the dependent interfaces. That's why you get this fatal error.
I'd suggest you setup autoloading in your application. For an example how to set this up you can have a look into init_autoloader.php from the ZendSkeletonApplication.