Unable to use array from external file - roots-sage

I’ve added a file inside App/icons/icons.php which has an array
<?php
$skills_icon = array(
‘one’,
‘two’,
‘three’
);
I have added the icons.php file in functions.php
array_map(function ($file) use ($sage_error) {
$file = "../app/{$file}.php";
if (!locate_template($file, true, true)) {
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
}
}, ['icons/icons','helpers', 'customizers/intro', 'customizers/skills', 'widgets/skills']);
and in my views home.blade.php I want to recieve array values values from it.
{{ var_dump($skills_icon)}}
But im receiving NULL values.
Anything I'm missing?

If you are sure that your file is loaded, have you try putting your data in a method that returns the data, and then call this method?
You could also add your file as a config/file.php, by adding it at the end of the function.php file with the others. And then call it with config() helper normally.

Related

Passing variable to .blade.php by return on Controller

I'm trying to pass a varible from the Controller to my .blade.php file.
I'm returning the view and compacted variables to the .blade.php but it doens't recognize the
variable.
This is the code of the Controller.
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison')->with(compact('contents'),$contents)->with(compact('contents2'),$contents2);
And i'm trying every way just to get an result but instead i'm getting the "Undefined variable $contents" page. The last method i used was a simple
<p>{{$contents}}</p>
I don't think it's correct but i don't really remember how to do it.
In controller return like:
return view('comparison.comparison', compact(['contents', 'contents2']);
And make sure your file is in resources/views/comparison/comparison.blade.php
Try this
$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));
return view('comparison.comparison', compact('contents','contents2'));
if you have defined a veriable above just use tha name inside compact as above and it can be acced inside blade as <p>{{$contents}}</p>
You can pass variables like that it's not mandatory to use compact.
return view('comparison.comparison', [
'contents' => $contents,
'contents2' => $contents2
]);
or if you want with compact:
return view('comparison.comparison')->with(compact('contents', 'contents1'));

Laravel attach a non-physically saved file

I'm trying to find a way to attach a non-physical file to an email that's being made on the spot.
I'm using the following lines in my controller:
$columns = ['line1', 'line2'];
$rows = [
['line1' => 'first', 'line2' => 'second'],
['line1' => 'first', 'line2' => 'second'],
['line1' => 'first', 'line2' => 'second'],
];
Mail::to('email#email.com')->send(new ExportMail($columns, $rows));
And the following build function in the ExportMail class:
public function build()
{
$file = fopen('php://output', 'w'); // Tried w+ too
fputcsv($file, $this->columns);
foreach ($this->rows as $row) {
fputcsv($file, $row);
}
// I tried a couple of things:
return $this->view('emails.myTestMail')
->attach($file);
return $this->view('emails.myTestMail')
->attach(fopen($file, 'r'));
return $this->view('emails.myTestMail')
->attach(fopen('php://output', 'r'));
return $this->view('emails.myTestMail')
->attach(file_get_contents($file));
return $this->view('emails.myTestMail')
->attach(file_get_contents(fopen($file, 'r')));
return $this->view('emails.myTestMail')
->attach(file_get_contents(fopen('php://output', 'r')));
}
But none if it works so I'm beginning to question if there is a way to send an email with a file that is never physically saved.
You can attach file data directly with the attachData method documented here:
https://laravel.com/docs/9.x/mail#raw-data-attachments
I use this regularly to attach dynamically generated PDF files, for example.
You should be able to do something like this:
return $this->view('emails.myTestMail')
->attachData($yourCsvData, "attachment.csv");
Also, I think you want to look at how you are generating your CSV data. Right now using php://output the CSV data will be sent out to the browser immediately, not stored in a variable.
There are a couple ways you can solve this, output buffering being one, or using php://temp instead, or using one of many CSV libraries (like https://csv.thephpleague.com/). I put together a fully working example for you here using php://temp:
https://laravelplayground.com/#/snippets/aa5b6594-4493-4ca4-9d12-837c102b7cc5
Expand the rawAttachments attribute on that Message on the right, and you'll see the attached CSV file.

Access $_FILE['tmp_name'] from the UploadedFile class?

if I print the content of an instance of UploadedFile, this is what I get
array (
'opt_image_header' =>
Symfony\Component\HttpFoundation\File\UploadedFile::__set_state(array(
'test' => false,
'originalName' => 'triangle-in-the-mountains.jpg',
'mimeType' => 'image/jpeg',
'size' => 463833,
'error' => 0,
)
And this is how I get the uploaded file in the Controller. Before of moving it, I should resize it.
foreach($request->files as $uploadedFile){
$ext = '.' . $uploadedFile['opt_image_header']->guessExtension();
$filename = sha1(uniqid(mt_rand(), true)) . $ext;
$uploadedFile['opt_image_header']->move($path . '/images/', $filename);
}
so there's no the "tmp_name" that I'd need for resizing the image before of saving it.
Do I need to read it directly from the $_FILE array?
Use $uploadedFile->getRealPath()
Symfony\Component\HttpFoundation\File\UploadedFile extends Symfony\Component\HttpFoundation\File\File, which extends PHP's SplFileInfo, so UploadedFile inherits all methods from SplFileInfo.
Use $uploadedFile->getRealPath() for the absolute path for the file. You can also use other methods on it, such as getFilename() or getPathname(). For a complete list of available methods (of SplFileInfo), see the docs.
Symfony's File class adds some extra methods, such as move() and getMimeType(), and adds backward compatibility for getExtension() (which was not available before PHP 5.3.6). UploadedFile adds some extra methods on top of that, such as getClientOriginalName() and getClientSize(), which provide the same information as you would normally get from $_FILES['name'] and $_FILES['size'].
If you are uploading a file with Doctrine, take a look at Symfony Documentation Upload a file
If you want to upload a file without Doctrine, you can try something like:
foreach($request->files as $uploadedFile) {
$filename = $uploadedFile->get('Put_Input_Name_Here')->getClientOriginalName();
$file = $uploadedFile->move($distination_path, $filename);
}
If there was any issue for uploading file move() will throw an exception
UPDATED
According to get the temp path of the uploaded file to resize the image you can use getPath() function in the mentioned loop
$tmp_name = $uploadedFile->get('Put_Input_Name_Here')->getPath();
If you ask why, because the Symfony File class is extends SplFileInfo

pass custom variable/parameter from email template to phtml file

I am stuck in my custom code.
I want to pass a custom variable from email template to pthml file.
Edit file
app/code/local/Mage/Sales/Model/Order.php
in this function :
public function sendNewOrderEmail()
{
--- default code start ----
$mailer->setTemplateParams(array(
'order' => $this,
'test' => 'XXXXX',
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml
)
);
--- default code end ----
}
and then I put this code in New Order email template:
{{layout handle="sales_email_order_items" order=$order test=$test}}
template file located here :
app/locale/en_US/template/email/sales/order_new.html
and I am trying to get test variable Here:
app/design/frontend/default/default/template/email/order/items/order/default.phtml
like this: $test = $this->getItem()->getTest()
but not get success. Please let me know where am I wrong? or what to do need to access this variable in phtml file?
Your problem here is that the 'test' value goes to the main block Mage_Sales_Block_Order_Email_Items that uses "email/order/items.phtml" tempalte.
In there you can find the data using:
<?php $test = $this->getTest(); // or $this->getData('test') ?>
You can then add this data into a registry.
But a better way is to send set this info onto the order items before the email.
So, in email function before $mailer->setTemplateParams(); add a code like:
//$this = current order if you are in Mage_Sales_Model_Order
foreach ($this->getAllVisibleItems() as $item) {
$item->setData('test', 'test_value_10');
}
To get variable in your email template
{{var test}}
Take a look # Defining Transactional Variables

Codeigniter multifile upload

$this->upload->display_errors()
shows the errors for all the files I am trying to upload.
How can I show the error for each file field ?
for example, if the user can upload 5 files and the third file is to big, I want to be able to tell him that this specific file is to big.
The functionality you are after is not built-in to the CI upload library. Here's CI's $this->upload->display_errors() method:
function display_errors($open = '<p>', $close = '</p>')
{
$str = '';
foreach ($this->error_msg as $val)
{
$str .= $open.$val.$close;
}
return $str;
}
It returns all the errors as a single string. You would need to adjust this accordingly, perhaps to return an array with the key for the respective file field.
You could use the $_FILES superglobal before hand, and check each field for errors, and then if there are no errors, call CI's do_upload()

Resources