How to implement composer in yii 1? - php

Hello I have old project written in php framework Yii 1.1. Dependencies are added manually by uploading extensions in extension folder.
I want to make my project use composer to track third-party code into vendor directory. So "extensions" directory would not exist.
Existing extensions are not namespaced and are used manually in controllers: Yii::import('application.models.black_lists.domains');
So is this possible to achieve and how?
Thanks

You have to require the composer autoloader before any vendor is used in your code.
To do so, you have to unregister Yii autoloader before you require the composer one
spl_autoload_unregister(array('YiiBase','autoload'));
require Yii::getPathOfAlias('application.vendor').DIRECTORY_SEPARATOR.'autoload.php';
spl_autoload_register(array('YiiBase','autoload'));
After this, you should be able to call any class in the vendor folder, the composer way.
new \Owner\Module();

Related

Install Slim 3.x manually

I want to use Slim 3.x framework to create a REST API for my applications.
I am using a shared hosting, so I can't use composer to install dependencies, therefore I have to install it manually.
I followed this tutorial, but I can't figure out how to change these lines in my index.php file to make it work!
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
and also this one:
use \Slim\Slim;
Slim::registerAutoloader();
Thanks!
The easiest way to handle Composer dependencies is to run Composer locally and commit the vendor directory into your repository.
Write your website, using Composer, as usual and commit composer.json, composer.lock and all the files in vendor.
Note the following:
Ensure that your .gitignore file does not exclude vendor. This is very common when starting from a skeleton project.
2, Ensure that you only use packages that have a release number. That is never use dev-master in your composer.json as if you do, Composer will install it via git and you won't be able to add it to your own repository. There are good reasons for avoiding dev-master dependencies anyway.
Your git repository now has all the files needed to run the website directly within it and so you can now simply upload your website to your shared host as you usually do.
I wrote up full details here: https://akrabat.com/using-composer-with-shared-hosting/
Download the Slim Framework from here https://php-download.com/package/slim/slim then just add require_once('vendor/autoload.php');

Autoloading 3rd party composer packages into Laravel 5 Application

I am trying to get my head around Composer. I want to integrate this package into my app: https://github.com/thiagoalessio/tesseract-ocr-for-php
I have done the following so far:
composer require thiagoalessio/tesseract_ocr
composer dump-autoload
and I have used the library within a Controller method as such:
$tesseract = new TesseractOCR($url);
But I'm getting the dreaded:
Class 'App\Http\Controllers\TesseractOCR' not found
How do I make sure that composer autoloads the package and that it is available throughout the apps, for usage within controllers?
Sorry, newbie in Composer here..
Composer has auto-loaded the package, you’re just referencing the class name incorrectly.
TesseractOCR lives in the “global” namespace, so you need to import it:
use TesseractOCR;
You can then use it in your controllers etc as normal.

"plugin" composer project, dependencies to be able to access main project

So I can't quite figure this one out. We have an API as a composer project.
No we which to add functionality to this API in a modular fashion creating separate composer projects for each module.
But how do I resolve dependencies while developing? Each module need access to the "core" API project to be able to test out code.
The current API is not a real composer package yet. But my initial thought is to create a new package for the module I'm about to develope, and then add a dev-dependency for the "core" API.
Or how should I do it?
Are your modules standalone resp. without the "Core" package usable?
I suppose not, so the "Core" module is a dependency for each of your modules.
Ergo the "you/core" package needs to be in the require block of composer.json of every module.
Note that this is not a dev-dependancy then, because i suppose your module wouldnt be usable without the core package.
Phpunit is a classic dev-dependency because the functionality of your module would still work if there would be no phpunit.
To test & develop the module, you would run a composer install in your module project folder to fetch all dependencies into a vendor folder. Then you can develop and have all your dependencies present. You would need to require the composer autoloader, though. F.e in your phpunit.xml.dist.
Dont forget to add vendor to your .gitignore

Proper directory structure for composer packages that require other composer packages?

Say for example if I want to create an MVC composer plugin that require Doctrine ORM, I would add doctrine to that plugin's composer.json's "require", right?
Now what if I wish to create a project that require that MVC composer plugin that I just created. Do I need to add doctrine to this project's composer.json's "require"?
In Symfony it does seem I would need to include Doctrine ORM again, but is there anyway possible to make this inclusion recursive somehow?
Thank you!
Edit
It was probably caused by my mistake in directory setup. Let me know if that is the case, here is my setup:
App
-- my app classes (Controllers, Models, etc.)
library
-- my
-- library
-- Class
composer.json (in here I have require doctrine)
vendor
composer.json (I do not require doctrine here,
should doctrine be added to my project when I do composer update in this case?)
Composer will resolve dependencies of dependencies, that's exactly the point. You can simply require a component, and that component can require other components etc. ad infinitum, and Composer will sort it all out and install all required dependencies.

Download a repository in Github along with composer packages

Without using Composer, is it possible to download a repository in Github along with it's defined composer packages?
For example: FluxBB 2 requires Laravel 4.
I was hoping to download FluxBB and at the same time the packages of Laravel 4 without using Composer.
Usually projects that use composer will ignore 3rd party components. In .gitignore you will see /vendor. This is the place where Composer downloads its dependencies.
This will find the latest version of monolog/monolog that matches the supplied version constraint and download it into the vendor directory. It's a convention to put third party code into a directory named vendor. In case of monolog it will put it into vendor/monolog/monolog.
Tip: If you are using git for your project, you probably want to add vendor into your .gitignore. You really don't want to add all of that code to your repository.
http://getcomposer.org/doc/01-basic-usage.md#installing-dependencies
Doing it manually is a bit of a hassle. Composer uses packagist to get its files (if you look at a package it has a source added to it Laravel https://packagist.org/packages/laravel/framework).
Composer auto loads the needed files automatically so its a big time saver.
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and you will get autoloading for free.
require 'vendor/autoload.php';
This makes it really easy to use third party code. For example: If
your project depends on monolog, you can just start using classes from
it, and they will be autoloaded.
http://getcomposer.org/doc/01-basic-usage.md#autoloading

Categories