How to add method to vendor class? - php

Could you tell me guys how to add a new method to vendor class for example for:
vendor/illuminate/html/FormBuilder.php
Whats the proper way to extend my laravel classes? I guess craeting new class somewhere in app folder?

The best way to extend the actual Laravel packages is to create your own package that require and extends (in your case) the Illuminate FormBuilder object. First check out the official docs here.
Be aware that the Form Builder is deprecated and no longer supported in the last Laravel releases. You can use the LaravelCollective Package instead.
So, let's recap... To achieve your goal you should:
Create your own package that requires the LaravelCollective component
Extend the LaravelCollective FormBuilder adding your own methods
Register your package into Laravel Container binding your own Service Container

Related

is Collection a Laravel instance or PHP instance?

is collection a laravel instance or PHP instance.
I always hear people say a Eloquent return a laravel collection so I want clearance on this
The Collection class in Laravel is a part of the Laravel framework. Therefore, if you attempt to use the Collection class in a pure PHP project (without the use of Laravel) you will not be able to use the Collection class by default.
You can make use of the Collection class without using all of Laravel. You'll see it's part of the Illuminate/Support components on github. https://github.com/illuminate
There are also composer packages that will install the Collection class for you all on it's own. https://github.com/tightenco/collect. So, I don't think it's correct to say it's a "Laravel Instance", but unless you install it on your project, you won't have access to the class without Laravel.
Laravel makes use of a lot of components that Laravel did not write on it's own. Symphony, is a major example of this.

Where to Place My Package in Lumen Framework

If I want to use some custom class in Lumen, where should I place them? The Laravel official document does not mention this in any of application structure, service container or package development. Actually I found the document quite confusing to some extents.
For example, I want to set up a service called Invitation, I know I need to register this class in InvitationServiceProvider but where should I place the Invitation.php which the actual class exists in? This package is used for some specific application thus I do not want to put it in composer packagist.
BTW, the version of Lumen Framework is 5.2.
So finally I created a folder named Service under app and just pull all the libraries inside...

Using own database wrapper in Symfony

Recently I started learning some Symfony as a side "project". I know with Doctrine you can use MySQL. However, I got my own database class using PDO. How can I implement and use my own database class on Symfony? And in which folder should I place it?
I used the download from here: http://symfony.com/download and the tutorial from http://symfony.com/doc/current/book/installation.html. I couldn't find anything about using an own database class.
You can use it like any other class in php, or you can create symfony service for that class (you have everything about services in symfony2 documentation)
According symfony2 best practices you can put your class in any folder in AppBundle. But if you want to reuse your class on multiple projects you should put it on packagist, and instal it with composer.
But if just you want to learn symfony you should consider using doctrine, because most of the symfony projects are using it.

Install script using Laravel models, possible? [duplicate]

Is it possible to use Eloquent without Laravel or does somebody knows a equally easy to use ORM?
Yes you can. A while ago Dan Horrigan released a package called Capsule for Laravel 4 which allowed Eloquent to be used independently and with minimal setup. The package itself has been merged with the L4 core so you no longer need to use the package.
If you refer to the illuminate/database repository there is a nice little introduction on using Eloquent without the framework.
Here is a small excerpt of getting it up and running.
$capsule = new Illuminate\Database\Capsule($config);
$capsule->bootEloquent();
$capsule->connection()->table('users')->where('id', 1)->first();
Update
Dan Horrigan has since removed his Capsule implementation as it is now built directly into Eloquent. Refer to the above illuminate/database link for more details on how to use Capsule.
Check out https://github.com/Luracast/Laravel-Database it provides full eloquent support including artisan migrations and more for the latest Laravel 8.* components.
It uses capsule and lazy loads the components when they are used.
Disclosure: I'm the author of this repository
In Laravel 4.*, Eloquent is automatically independent because it's shipped with Dan Horrigan's Capsule. You don't need to download any extras. For a how to please visit: https://github.com/illuminate/database/blob/master/README.md

php composer reuse of modify package

Using composer, I have come across a package that does what I want, if I modify the constant values and some of the code in private methods.
What I've been trying to do is create my own package and extend the other package that I've found and try to override the methods from within my own classes. However, as the package contains constants and private methods, it's difficult/impossible to implement my own package to take advantage of the other package.
What I'm wondering now is what is the best way to go about using the code in this other package? Do I copy the code into my own package and change the namespace and modify the code for my own needs? Or is there another way where I can change the values in another package and modify the private classes?

Categories