Using composer to replace MVC Framework - php

i'm building a new project on top of CodeIgniter MVC framework, but now that i'm looking at the design, i will actually not use most of the framework features except for 1.form validation, 2.router and 3.session/cookie/input handling, and 4.views generation
even for database i will use Redbeans php ORM.
So my question
how can i glue these components that i fetched with composer from other frameworks so i can inject them into my project and be good to go without other extra stuff that i don't need ?
would it be a wise decision ? or is there any risks of depending on components rather than whole framework ?
i have googled a lot but it seems like i'm using the wrong keywords or something, i could not find any tutorial except for this one -which dictate symphony- that teach you how to do this and what are the risks of doing it

You can take a look at Packagist to explore which components Composer offers, RedBeanDB is definitely in there. However, in contrast to Symfony, CodeIgniter is not really Composer-friendly. So if you are looking for a proper way to just load these few CodeIgniter components as a dependency, I guess you're out of luck.

To keep from digressing, I'm simply going to answer your two main questions:
1) There are plenty of tutorials out there on how to use composer to grab your packages, so I wont do a full writeup, but basically what you need to do is download composer, create a composer.json file, then run composer to have it download all those packages and their dependancies. All you then need to do is in one of the already loaded codeigniter files, autoload the autoload.php file
`require 'vendor/autoload.php';`
Check the composer documentation here on how to download composer and setup your json file.
2) Definitely. The Laravel framework is actually built this way under the hood, instead of writing everything themselves, they use composer to pull in packages from other projects like Symfony to do certain tasks.

Related

Reverse engineering a composer.json file based on existing project library?

This is probably a pointless question but figured theres no harm in asking.
I have inherited a website which from what I can gather is using some Zend libraries and Doctrine... I think, excuse my ignorance as I'm not very clued up on the Zend framework.
In anycase I have been asked to update the numerous instances of depreacated code in among it. Which I thought would just have been within the application code so I could just update functions here and there etc etc.
It turns out most of the code needing updated is within the library folder which from what I can gather is all libraries which I would have thought would have been set up using composer but theres no mention of a composer.json file within the codebase.
So im now at a loss as I have a bunch of libraries that I have no idea how to update. Which brings me to my question, what would be the best way to attack this problem?
I was thinking if there was someway to reverse engineer a composer.json file based on the contents of the aforemetnioned library folder, that would be a good start as I could then use that as the basis for replacing the library folder with the updated libraries pulled in by composer but I have a feeling im probably clutching at straws.
To give some idea, this is the top level folder structure of the library folder:
Given the amount of subfolders within those listed and all the dependencies no doubt involved I would imagine manually updating them would be a nonstarter.
Would be interested to get thoughts on the best course of action here.
If I were in your spot I would create a composer.json and add try to add the libraries from the folder one by one and check if the app still works and fix dependencies one by one, I also see there is a vendor folder in there, that may have been the result of a composer.json which could be easily added in a new composer.json, but you will have to take the libraries one by one.

How to extract config/views while developing a package for composer/packagist?

I am creating a stand-alone package at packagist.org, seen this article and it helped me a lot http://blog.jgrossi.com/2013/creating-your-first-composer-packagist-package/. Can you please help me out how can I generate views/config like I want to extract some files outside the vendor directory when someone install my package from packagist by composer command?
If you want to use views in your PHP package you should choose what view parser you want to use, or you can even create your own, using ob_start(), etc.
I suggest you to take a look how packages do that, maybe checking the Laravel View component. It's not difficult to do that, but once you're creating a package for PHP and not for a specific framework you have to choose how you're going to implement views, just that.
Cheers, JG.

Automatically discover composer packages

I'm wondering what the best way (if there is a way) for an application to auto-discover [relevant] PHP "packages" installed by Composer.
My use case specific scenario:
I have a PHP app that includes my "framework" (for lack of a better word). This framework brings some basic functionality (routing, admin etc).
I'm slowly building in more advanced functionality, say, a blog module. This module is entirely self contained in it's own directory (but obviously has dependencies on the framework).
I'd like this blog module to be a self contained Composer package, so that I can selectively require the package in my app's root composer.json file.
Now, I need for the framework to know that it's there so that it can, for example, set the routing correctly and load up any admin functionality that the module requires.
What I've thought so far:
I'm relatively experienced in PHP, but "proper" OOP and autoloading is a little bit beyond my knowledge at the moment, so please forgive if there are inbuilt functions to do this. I don't even know what terms to Google!
I have thought I could maybe read the installed.json file which composer puts at vendor/composer/installed.php but I'm not sure how to set up my packages (e.g. blog) so they announce what they are. I'd like to future proof it so that I'm not looking for known module names (or regexing vendor or package names), but rather looking for packages to say "hey framework, I know you! You can use me!"
Maybe I can somehow instruct Composer (through the package's composer.json file) to stick in an arbitrary key/value pair in installed.json?
Any suggestions welcome, or directions as to what sort of Googling I should be doing.
Oh welcome to the world of managing dependencies on your framework.
I have some experience with auraphp, where we dealt with similar issue. You can read the blog post Composer-Assisted Two-Stage Configuration .
So what we finally ended-up adding https://github.com/auraphp/Aura.Web/blob/a3870d1a16ecd3ab6c4807165ac5196384da62cd/composer.json#L26-L36 these lines in the packages that need to understand to load by the framework.
You can also see how this bundle can also get autoloaded with the configurations.
in your composer.json
https://github.com/harikt/Aura.Asset_Bundle/blob/6ea787979390e69bf6ecb1e33ce00ed90f306e2f/composer.json#L21-L27
and the config/Common.php ( https://github.com/harikt/Aura.Asset_Bundle/blob/223126cedb460e486c4f0b242719c96c14be5385/config/Common.php ) , note we have other development modes also. For a detailed look check https://github.com/auraphp/Aura.Web_Project or https://github.com/auraphp/Aura.Framework_Project
Hope that helps a bit to look into the code and work on your own solution.

Using Composer, can dependencies be shared between multiple projects?

First of all, I'm a complete newbie to Composer. I've been trying it out since it's a sounds awesome and mainly because Zend Framework 2 uses it.
Zend Framework 2 is actually also the reason for this thread.
It get the basics of Composer. But with my current server setup I have a request, which I can't seem to figure out if possible.
Let me explain.
I have multiple Zend Framework 2 projects:
/home/morten/sites/Project-1/
/home/morten/sites/Project-2/
/home/morten/sites/Project-3/
All of these projects should be running ZF2. When running composer in each project - each of them get their own separate download of the ZF2 Library files. Which is a bit redundant with my setup.
I have a complete and up-to-date download of ZF2 Library located at:
/var/www/shared/Zend/
And my php.ini has that path added to PHP's include_path, so the whole ZF2 library is available for all the three projects.
IS IT POSSIBLE to make Composer use in it's setup. Because if I try to change stuff and try things out in the composer files, then it just re-downloads Zend because it's a required component for other modules.
Can this be done? And if yes, how can I do it?
Hope I have explained myself good enough for you guys to understand what I'm trying to do and want :)
Thanks a lot in advance!
Regards,
Morten
You might be able to have one composer.json file stored in /var/www/shared/Zend, where you would put your dependencies and use Composer do manage them. Then all projects could include the same autoloader.
However, I wouldn't recommend that approach:
Your project's dependencies (composer.json) should be stored with your project. If you want to install your project somewhere else (for instance if you want to move one project to another server), you are missing the composer.json to install the required dependencies.
It will not be possible to update the dependencies of one project, without updating the dependencies of all other projects. If you want to introduce a new feature in Project 1, which requires a new version of a certain dependency, all other projects will get this new version as well - which might introduce backward compatibility breaks if an older feature that Project 2 relies on, is no longer supported. Since you can only have one version of each dependency, it is not possible for two projects to have different versions of the same dependency.
This approach might be useful if all projects will have the exact same functionality and code, but in that case you should probably merge them into one project.
In all other cases, I would suggest to use Composer the way it's supposed to be used: give all projects their own composer.json file and let Composer download the dependencies per project, in each project's vendor directory. The downside is that it'll cost you a little more disk space, but you'll get a lot of flexibility for it in return.

Best Way to Install Zend Framework 2

I'm new to the Zend Framework but have used PHP in the past. I've gone through the basic tutorial provided by Zend, and understand a lot of it, but I'm fuzzy on the installation process. There seems to be many different ways to install, is that right? Here are the ways I've seen:
Download .zip file and manually extract
Use command line with composer.phar
Use command line with zftool
If someone could give a brief explanation of each of those above methods, how they differ, and which one is better, that would be great!
Also, two more quick questions on topic:
The tutorial mentions including Zend in your PHP include path. Why would you need to do this if the framework is included in the projects directory?
Is there a way to install the framework without the skeleton app?
Thanks in advance!
I'll only go about the 2nd set of questions:
You don't. You either have it inside your include path or inside your projects.
php composer.phare require zendframework/zendframework 2.2.*
Your first question is rather opinionated and it heavily depends on your workflow. To quote some song lyrics:
Sei ying there is no special move that is best, it all depends, any move could be best,
its up to you when the times right, to move correctly, accurately and with great speed...
tl/dr choose the method that seems most practical to your solution.
Try this website: http://www.packtpub.com/article/building-your-first-zend-framework-application
It shows how to install, add a virtual host and skeleton application and add modules to that application.
Happy learning
Use Zip if you dont have access to other methods (composer or git). With Zip you wont be able (easily) to upgrade or downgrade components.
composer is great because you can configure your needs. for example:
"require": {
"zendframework/zend-config": "2.2.*",
"zendframework/zend-http": "2.2.*"
},
More info: http://framework.zend.com/downloads/composer
Use Git if you wish to be on the edge (more hard to maintain versions)

Categories