Laravel: Edit file in the vendor directory - php

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(..)

Related

Laravel5.6 - override a vendor view

I try to make my own version of a vendor blade template.
I dont want to extends the controller with the reference of the view.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(__DIR__.'/../../resources/views/oauth/passport', 'passport');
I created a file named authorize.blade.php in /resources/views/oauth/passport
In the vendor controller method we can see this:
return $this->response->view('passport::authorize');
The problem is when I call the vendor controller method it loads his version of authorize.blade.php. I would like mine to be loaded and I expected the new line I added to AppServiceProvider to do that.
Passport comes with VUE components and views you need to publish first to override them. From the Laravel Passport page:
"If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command."
All you need to do is run php artisan vendor:publish --tag=passport-views and the vendor views will be place in resources/views/vendor/passport, where you can edit them.
Use can use php artisan vendor:publish --tag=passport-views this will copy the views to your views folder for you to change.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(DIR.'/../../resources/views/oauth/passport',
'passport');
You can use this option only by placing it in registry () instead of boot(). And then you can use your Views regardless of whether they were published in Vendor or not

How can i rename laravel controller with command line interface(CLI)?

I created controller which name is Home but I want rename it with HomeController. Is it possible in laravel?
Example:
class Home extends Controller { }
I Want to rename it like
class HomeController extends Controller { }
I Want to do it with CLI.
You can't use artisan, but you can do it manually. But it's not just a simple rename.
You have to:
Rename the PHP file.
Edit the PHP file to give the class the new controller name (the class name and the file name must match)
Change any code that uses that old class name to use the new class name
Edit the autoload files to load the new controller file:
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php
You have to do it manually, there is no any command available to updated controller name.
Make a new Controller with the new Name.
php artisan make:controller NewController
or
php artisan make:controller NewController --resource
//if it is a resource controller
Now copy all contents from Old Controller to NewController.
Then edit all routes (in web.php) to that Controller.
forget CLI why not try rename the controller using 'AS'
use app\MyController as MyControl;
I think doing composer dump-autoload this regenerates the files :
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php
You can assess the file using nano command or vi, it depends on your machine. it basically an editor the enable you to change the content of the file.

Use Composer module in PHP file

I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.

How do I add a Laravel command in a subfolder?

So I'm trying to create a custom console command in Laravel 5.1 which does some helpful function for my project. I can do this fine when putting the console command in a file located at the base commands folder but not when I just to add a subdirectory.
'App\Console\Commands\SomeCustomCommandThatWorks',
'App\Console\Commands\MySubNameSpace\CustomCommandThatFails',
So how do I add my command like MySubNameSpace\Command?
Namespace doesn't appear to have any effect on this. The namespace of the command could be App\Console\MySubNameSpace\MyCommand or App\Console\MyCommand both fail if the file is located at 'App\Console\Commands\MySubNameSpace\MyCommand'. The file also fails if located at 'App\Console\Commands\MyCommand' with namespace App\Console\MySubNameSpace\MyCommand.
Right now I get this error.
Class App\Console\Commands\DeletePhantomServers does not exist
I have tried running composer dumpautoload but to no success.
Any help would be appreciated.
According to autoloading standards, none of the combinations you have above will work. The namespace needs to be set according to the directory. So if you have the command in this folder
Console\Commands\MySubNameSpace\MyCommand
your namespace has to be
App\Console\Commands\MySubNameSpace\MyCommand
I must be tired. After reading Michael's answer I looked at the namespaces and realised I was just simply writing them wrong.
I was putting
namespace App\Console\Commands\MySubNameSpace\MyCommand;
class MyCommand ...
Where I needed to put
namespace App\Console\Commands\MySubNameSpace;
class MyCommand ...
Thanks for the help people :)

how to call modules' model and controllers files in a controller in laravel 4.1

I created a modular system by using laravel 4.1 I have a tree scheme as follows:
app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php
What I want to do is to call the model from a controller in app/controllers/ class.
How I can call that module and its model?
Make sure your /app/modules is added to your composer.json file's autoload : classmap and issue composer dump-autoload or php artisan dump-autoload. Then you can just create an instance like new ModuleModel or whatever name you gave your class. Though it's better to pass to your controller by dependency injection. This way your code will be easier to test because you can pass in stub data.
public function __construct(ModuleModel $module_model_instance) {
$this->module_model_instance = $module_model_instance;
}
I'd rather add this as a comment but have insufficient rep.
If everything is correctly autoloaded by composer in the PSR-0 (or 4) section, then you should just be able to reference it using it's namespace?
I sometimes need to run
composer dump-autoload
to refresh the autoloaded files, especially if using vagrant.
Hope this is helpful. I'm not sure if I've fully understood your problem.

Categories