Laravel File equivalent in Lumen - 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.

Related

Laravel - Load different .env file

I need to load a different .env file, named .env.test under certain conditions.
I attempted to do that through a middleware by adding
app()->loadEnvironmentFrom('.env.test');
Dotenv::create(base_path(), '.env.test')->overload();
to the bootstrap() method of Kernel.php. I also tried to create a dedicated middleware for this and load it as the first one in the web middleware group. But either way, it is loading the standard .env file.
It works if I do it in the /bootstrap/app.php file but I really don't want to put it there.
I just figured it out: The default .env file is being loaded inside of the bootstrap() method of LoadEnvironmentVariables.php.
To use the .env.test file I had to restructure my initial bootstrap() method inside of the App/Http/Kernel.php file to look like this:
public function bootstrap()
{
app()->loadEnvironmentFrom('.env.test');
parent::bootstrap();
}
So the essential part was to move the parent::bootstrap() call below the loadEnvironmentFrom() call.
Instead of doing any code change, you can use export command create a file called .env.test, you want to sue this as .env file use terminal
APP_ENV=local
php artisan config:cache
php artisan key:generate
This below edit is to explain how .env file is getting set
In Illuminate\Foundation\Application class has method loadEnvironmentFrom which is taking the file as parameter and setting it,
you can use bootstrap/app.php after you are getting $app
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
here you will be having instance of Illuminate\Foundation\Application
you can just call the loadEnvironmentFrom function like
$app->loadEnvironmentFrom('.env.local');
May be it is better to use Kernel.php instead of this, I do not think either of bootstrap/app.php or kernel.php will get overridden with composer update, so make more calculation while using it. I have added this, so that it will help you understand the stuffs.
You can load a different environment file using APP_ENV
For example if APP_ENV=test then .env.test can be loaded.
More info: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php#L41

I am not able to `use` or `namespace` in laravel helper file

I have created helper file in laravel. I want to use laravel functionalities.
I am not able to use use or namespace template in helper file.
code of helper file.
namespace App\Helpers;
class CommonFunctions {
public static function get_cat() {
}
}
I have linked this helper in config/app.php
When I use namespace in helper file then this error is displaying
Blade file code
Finally I found the issue.
Actually I am migrating site from laravel 5.2 to 5.7 and I was copying the old helper file to new that's why I got this error..
Previously I was using use and namespace together but after adding use statements only I solved the issue.
It might be the issue of namespace is not allowing in helper file, but in every solution and method of creating helper files there is namespace, so, I had never try to remove that line.
But now I got the solution...
Thanks for all who helped me.
Shishil Patel
I guess you have syntaxis error in your helper.
anyways try to execute php artisan config:clear and then fix all errors.
I have done everything the same way and I haven't found any problem.

How to set require path in Laravel controller?

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

Laravel: Edit file in the vendor directory

I am working with laravel and I have installed a package using composer by running this command composer require mailchimp/mailchimp=~2.0.
After that I got a folder 'mailchimp' in the vendor directory. In there, there is a file named Mailchimp.php that I have to modify, but based on some old posts here, if I modify the file, any time I run the command composer update, I will loose my changes in the file, just because it is located in the vendor directory. So is there any option for me to solve this problem ?
I tried using the command php artisan vendor:publish but I do not get the expected results.
You can create a custom class which will extend the Mailchimp class and override the function you want. Then use the custom class in your code.
use DrewM\MailChimp\MailChimp;
class CustomMailChimp extends MailChimp {
...
// The function you would like to override
}
Then use it new CustomMailChimp(..)

Yii2 namespace autoloader doesn't find copied files

I am running the Yii2 framework locally, and I want to reuse an model I created in a earlier project.
So I copy the file TestForm.php to the models directory, change the namespaces from namespace backend\models to namespace app\models and try to create an object from it with:
$model = new \app\models\TestForm;
Which gives me
Unable to find 'app\models\TestForm' in file: /var/www/html/operators/basic/models/TestForm.php. Namespace missing?
Which is weird because the namespace is correct.
However, if I create the file TestForm.php myself and copy the contents of the older file over, everything works fine.
What's going on?
(I use ubuntu 15.04)
I think in your /models/TestForm.php you don't have specified the correct namespace eg :
namespace basic\models;

Categories