I have a CSV file named beatles.csv that has the following contents on each cell:
John,Paul,George,Ringo
In vanilla PHP, this code works without issue (CSV file is in the same location as the script):
$data = 'beatles.csv';
$file = new SplFileObject($data);
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
echo $row[0].'<br>'; // outputs John
}
In Laravel 5.4, I created a folder named MyDomainName under the app folder and I put the CSV file there so its namespaced as follows:
use App\MyDomainName;
And the content of my method on the controller which should fetch the CSV data is as follow:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\MyDomainName;
class PageController extends Controller
{
public function fetchCSV()
{
$file = new SplFileObject('beatles.csv');
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row){
dd($row[0]);
}
}
}
And I get this issue:
FatalThrowableError in PageController.php line 67:
Class 'App\Http\Controllers\SplFileObject' not found
I thought that by using use App\MyDomainName; it will solve the issue but still it didn't solve it.
I have also explicitly use the full path but still it didn't work.
$file = new \App\MyDomainName\SplFileObject('App\MyDomainName\beatles.csv');
and I still get the same error:
FatalThrowableError in PageController.php line 67:
Class 'App\MyDomainName\SplFileObject' not found
What could be the issue why is it that the SplFileObject class cannot be found?
In your fetchCSV method replace this code
$file = new \SplFileObject('beatles.csv');
I've faced the same use. You just need to add the forward-slash before SplFileObject()
thanks #Mark Baker, the issue is now solved:
public function fetchCSV()
{
$file = new \SplFileObject('../App/MyDomainName/beatles.csv');
$file->setFlags(\SplFileObject::READ_CSV);
foreach ($file as $row){
dd($row[0]); // outputs "John"
}
}
Related
I am trying to use a library of classes within Laravel 8. I am having difficulty getting the classes to load correctly. I created a new folder within the App folder called DigiSigner, which is the external library's namespace.
App\DigiSigner
DigiSignerClient.php
\libs
BaseRequest.php
Branding.php
ClassLoader.php
Config.php
Curler.php
DigiSignerException.php
DigiSignerResponse.php
Document.php
DocumentField.php
DocumentFields.php
ExistingField.php
ExportObject.php
Field.php
SignatureRequest.php
Signer.php
I created a controller that looks like this
class SignPDFController extends Controller
{
public function getPDF()
{
$client = new DigiSignerClient('client_key');
$request = new SignatureRequest;
$request->setEmbedded(true);
$request->setSendEmails(false);
$template = Document::withID('document_id');
$template->setTitle('Site Title');
$request->addDocument($template);
$signer = new Signer('user#email.com');
$signer->setRole('Signer 1');
$template->addSigner($signer);
$initials = new ExistingField('key');
$initials->setContent('VS');
$signer->addExistingField($initials);
$response = $client->sendSignatureRequest($request);
foreach ($response->getDocuments() as $document) {
foreach ($document->getSigners() as $signer) {
$signDocumentUrl = $signer->getSignDocumentUrl();
}
}
}
}
The DigiSignerClient and the SignatureRequest classes seem to load fine, but the SignatureRequest needs to load the ExportObject class to extend it.
namespace App\DigiSigner;
use App\DigiSigner\libs\ExportObject;
class SignatureRequest extends ExportObject {
I end up with an error like the following.
Error Class 'App\DigiSigner\libs\ExportObject' not found
Namespaces and use are a little fuzzy for me. If someone can point me in the right direction, I would be delighted.
I believe I figured it out. All the files in the subdirectory needed the namespace changed to App\DigiSigner\libs.
Check ExportObject.php namespace.
I'm using PHPSpreadsheet to create Excel.
I want t generate the Excel file, then convert the Excel file in a PDF one.
So I've done the following :
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use PhpOffice\PhpSpreadsheet\Reader\Exception;
class DevisGenerator
{
public function runDevis()
{
$spreadsheet = $this->loadexcelTemplate();
$uuid = $this->uniqidReal();
$filename = $this->writeName($spreadsheet, $uuid);
$this->convertPdf($spreadsheet, $filename);
}
public function writeName($spreadsheet, $uuid)
{
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('B2')->setValue('Toto');
try {
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $uuid;
$writer->save($filename.'.xlsx');
}catch (Exception $e)
{
//TODO gestion erreur
}
return $filename;
}
public function convertPdf($spreadsheet, $filename)
{
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
But whan I run the code the following error appear :
Attempted to load class "Mpdf" from namespace "Mpdf".
Did you forget a "use" statement for "PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf"?
I did not understand this error, I have correctly insert the use statement in my code.
Any idea ?
I've already got a similar issue with Mpdf.
PHPSpreadsheet supports multiple Librairies to generate PDF.
I'm nut using Mpdf but Tcpdf.
I'm also not sure but you need to install them manually.
composer require tecnickcom/tcpdf
Then in your code :
$writer = new Tcpdf($spreadsheet);
And don't forget the use statement ;)
Hope this help !
After use statement before class, you should use only:
public function convertPdf($spreadsheet, $filename)
{
$writer = new Mpdf($spreadsheet);
$writer->save($filename.'.pdf');
}
Since you use the fully qualified namespace when creating the instance, the use statement is not taken into account (thus the error message).
It seems like you have added a supplementary slash at the beginning of the namespace when creating your Mpdf instance, removing it will solve your issue.
$writer = new PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
But since you have added a use statement, you do not need to use the fully qualified namespace again, you can do
$writer = new Mpdf($spreadsheet);
I'm attempting to store the email address of any user who uploads a file into an "uploader_name" string variable. I've attempted using the following code whichout success - attempting to upload the file will result in a Class 'App\Http\Controllers\Auth' not found error message being thrown back. I know this is a pretty basic question, but I can't seem to find any answers which specifically work with Laravel 5.
This is what I'm trying to use currently without success.
$filelist->uploader_name = Auth::user()->email;
If it helps, here's the full function which saves some of the automatic information of the file.
public function store()
{
$filelist = new Filelist($this->request->all());
//store file details - thanks to Jason Day on the unit forum for helping with some of this
$filelist->name = $this->request->file('asset')->getClientOriginalName();
$filelist->file_type = $this->request->file('asset')->getClientOriginalExtension();
$filelist->file_size = filesize($this->request->file('asset'));
$filelist->uploader_name = Auth::user()->email;
$filelist->save();
// Save uploaded file
if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
$destinationPath = public_path() . '/assets/';
$fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
$this->request->file('asset')->move($destinationPath, $fileName);
}
return redirect('filelists');
}
You get not found error, because you don't use the right namespace.
You can resolve this if you use the use keyword like this:
use Auth;
public uploadController {
...
}
Or you can use the full namespace:
$filelist->uploader_name = \Auth::user()->email;
You are getting Class 'App\Http\Controllers\Auth' not found error because you are not using the correct namespace:
There are two ways to resolve this:
By including use keyword in your controller
Example:
use Auth;
UploadController{
....
}
or by specifying the full namespace directly like so
$filelist->uploader_name = \Auth::user()->email;
I'm using WAMPSERVER 2.5 and PHP version 5.5.12 on Windows 8 PC. I created a namespace which worked ok when I was running PHP version 5.2.12. After upgrading to php version 5.5.12 I'm getting error message about undefined variables which I think means that the namespace is not being used. Here's what my code looks like:
In my UploadFile.php file I have this:
<?php
namespace myNamespace;
class UploadFile
{
protected $avatarUrl;
public function getUrl()
{
return $this->avatarUrl;
}
protected function moveFile($file)
{
$filename = isset($this->newName) ? $this->newName : $file['name'];
echo $file[$key];
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$url='http://westcoastchill.com/dc-esports/images/' . $filename;
$this->avatarUrl=$url;
...
}
.....
?>
Then here's the html form that uses the class in the namespace where I get the error messages that states that the variable 'newUrl' is undefined and the index 'displaymax' is undifined.
<?php
require_once 'uploads/src/myNamespace/UploadFile.php';//<------names here
if (!isset($_SESSION['maxfiles'])) {
$_SESSION['maxfiles'] = ini_get('max_file_uploads');
$_SESSION['postmax'] = UploadFile::convertToBytes(ini_get('post_max_size'));
$_SESSION['displaymax'] = UploadFile::convertFromBytes($_SESSION['postmax']); //<------ undifined index
}
$max = 50 * 1024;
$result = array();
if (isset($_POST['upload'])) {
$destination = __DIR__ . '/uploads/uploaded/';
try {
$upload = new UploadFile($destination);
$upload->setMaxSize($max);
$upload->allowAllTypes();
$upload->upload();
$result = $upload->getMessages();
$newUrl=$upload->getUrl(); //<----------- here's the undifined newUrl;
} catch (Exception $e) {
$result[] = $e->getMessage();
}
}
$error = error_get_last();
$oldUrl=$newUrl;
...
?>
}
How can I access my class from a namespace with php version 5.5.12?
Thanks for any help with this!
UPDATE: Sorry I had getUrl() outside of superclass but in my actual project is in the correct place. So I tried:
\myNamespace\UploadFile::convertToBytes(ini_get('post_max_size'));...
but I still get the same error. I also tried adding use:
myNamespace\UploadFile and \myNamespace\UploadFile...
still getting the same error message. The thing is my code worked with the using statement before I updated PHP so I'm curious why just an update would change things.
UploadFile class is within the namespace myNamespace, so to reference the class from outside of myNamespace, you would need to use \myNamespace\UploadFile.
If you are already in the global namespace, you don't need the leading slash, but I think it's good practice to always use the leading slash, since the leading slash refers to the global namespace.
Ex:
$_SESSION['postmax'] = \myNamespace\UploadFile::convertToBytes(ini_get('post_max_size'));
and
$upload = new \myNamespace\UploadFile($destination);
The issue was the configuration of the php.ini file. After researching more I tried turning off 'display_errors=Off in the php.ini file and the code the code ran using the namespace and everything. Conclusion: the php.ini file of the new installation different settings. I'll explore the settings more but my problem was solved by turning off error displaying. Obviously this is not a desired option so I'll toggle on and off as I go forward and read up on other parameters in the file that may help me.
I'm using Laravel and creating artisan commands but I need to register each one in start/artisan.php by calling
Artisan::add(new MyCommand);
How can I take all files in a directory (app/commands/*), and instantiate every one of them in an array ? I'd like to get something like (pseudocode) :
$my_commands = [new Command1, new Command2, new Command3];
foreach($my_commands as $command){
Artisan::add($command);
}
Here is a way to auto-register artisan commands. (This code was adapted from the Symfony Bundle auto-loader.)
function registerArtisanCommands($namespace = '', $path = 'app/commands')
{
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->name('*Command.php')->in(base_path().'/'.$path);
foreach ($finder as $file) {
$ns = $namespace;
if ($relativePath = $file->getRelativePath()) {
$ns .= '\\'.strtr($relativePath, '/', '\\');
}
$class = $ns.'\\'.$file->getBasename('.php');
$r = new \ReflectionClass($class);
if ($r->isSubclassOf('Illuminate\\Console\\Command') && !$r->isAbstract() && !$r->getConstructor()->getNumberOfRequiredParameters()) {
\Artisan::add($r->newInstance());
}
}
}
registerArtisanCommands();
If you put that in your start/artisan.php file, all commands found in app/commands will be automatically registered (assuming you follow Laravel's recommendations for command and file names). If you namespace your commands like I do, you can call the function like so:
registerArtisanCommands('App\\Commands');
(This does add a global function, and a better way to do this would probably be creating a package. But this works.)
<?php
$contents = scandir('dir_path');
$files = array();
foreach($contents as $content) {
if(substr($content,0,1 == '.') {
continue;
}
$files[] = 'dir_path'.$content;
}
That reads the contents of a folder, itterates over it and saves the filename including path in the $files array. Hope this is what you're looking for
PS: Im not familiar with laravel or artisan. So if you have to use specific semantics(like camelCase) to register them, then please tell me so