In most case, we want to extend our Laravel applications capabilities by adding helper class.
It is not a best practice to insert this classess inside Controller and Model.
so we have to write our classess in separate files and group it by folder
Here let us have a look how we add helper classess in laravel
1. We have app folder on root folder of laravel.There we can create a Libraries folder inside app folder.
2. We can create a class/helper file and we can write a our custom class there, on the top of custom class we have to define namespace our_folder_name;
namespace our_folder_name
for example ,
//here my folder name is Libraries
namespace Libraries;
class className{
//class methodes
}
Now we can go to our controller and on top we can include our helper class
use Libraries\filename as filename;
And we can go to compser, inside autoloder we can add “app/foldername”
All my samples files are given below
Library/className.php
<?php
namespace Libraries;
class className{
//class methodes
function testmesthode(){
return "Hello world";
}
}
testController.php
<?php
use Libraries\className as className;
class testController extends BaseController {
function index(){
$test = new className();
echo $test->testmesthode();
}
}
Today i was trying to install laravel in MAMP , While going through their step on laravel site mentioned i found that i didn installed mycript extension. I had googled for lot of time and lastly i installed .
I am writing this will help some one to install laravel in Mac using MAMP.
Install composer
Before installing laravel we have to install composer . We can go to https://getcomposer.org/ and we can install from from there or we can follow the following steps step
1. first we have to check curl is enabled . step 2. we can install composer globally as mentioned in their site
Today we are going to learn about force downloading prompt in PHP.
While i am doing my projects, i had a requirement for file download, File type will be changed, So i made a research on this and made simple useful script which can be use any where to prompt download by giving path and file name . Let us create a function download_file and pass full file path and filename , we can use third argument to mention the MIME Type of file
Next we are going to set mime type if we already pass the MIME type into function it will take that MIME ,else we are going to use a function pathinfo to retrieve the extension of file and we will compare with already created array to get content type
lastly we are going to read each line by line and print that as shown below.
$chunksize = 1 * (1024 * 1024);
$bytes_send = 0;
if ($sourceFile = fopen($sourceFile, 'r')) {
while (!feof($sourceFile) &&
(!connection_aborted())
) {
$buffer = fread($sourceFile, $chunksize);
print($buffer); // is also possible
flush();
}
fclose($sourceFile);
} else {
header("HTTP/1.1 505 Internal server error");
return;
}
That is it !!!!! Now we can have a look, How we can read the file using created function We can make a path file and the name we want to give while downloading and pass to function . It will download prompt for file. example for reading file and download prompt