Skip to main content

How to use composer with PHP, Yii framework

 

Introduction:

Some PHP components is installed using composer, like every other dependency framework.
You can reference the page:  https://packagist.org/ . This page contains a lot of useful PHP repositories, libraries.

The benefits of using composer are:

  • When we create a library which depends on another libraries(of other developer), we of course do not want to copy all these libraries into our project(do not do that, they are not yours). We might want our project(library) to be condense, simple to use and always up-to-date.
--> Therefore, we will create a file, which presents that our library is depending on some other guys...
  • Our library users might want a small command to download everything they need, including our library. One command and everything is setup.
  • User want the latest version of our library

If you are a developer, how to install libaries using composer?

First, lets imagine that your project need some external libraries(this is the fact, not imagination). So, you need to declare that, of course, raise your voice when you are hungry.

>Step 0: Go to your project's root directory,

> Step 1: Install composer, using command line:

curl -s http://getcomposer.org/installer | php

After you install, a file composer.phar will be created

> Step 2 :  create a file named composer.json, this is the file which you declare about your project. The format of the file should be(simplest):

{
    "name": "your-vendor-name/package-name",
    "description": "A short description of what your package does",
    "require": {
        "php": ">=5.3.0",
        "another-vendor/package": "1.*"
    }
}

You declare your project name, description, and the most important part: require. This part tells what libraries your project needs

Your project requires many libraries, the format of a requirement is vendor/package:version.

> Step 3: I will tell you how to download a library. For example, we try  yii2-apns-gcm library.
Open the page and you will notice, there is a part:

    require: "bryglen/yii2-apns-gcm": "dev-master"
Lets copy it into your require part of composer.json.
> Step 4: Start downloading

php composer.phar install

> Attention: there might have some problems with library version(stable or dev). By default, composer only loads stable library only. If the library you want to download is depending on some library which is dev-master, composer would not download it.

Way to solve it is add this into your composer.json file:

    "minimum-stability":"dev" 

Reference: https://groups.google.com/forum/#!msg/composer-dev/_g3ASeIFlrc/Lu537ZXuOLgJ

Composer and Yii

Many Yii libaries use composer. With Yii, external libraries are saved in protected/extension or protected/vendors. You might need to declare this directory in your composer.json

        "config": {
             "vendor-dir": "protected/vendors"
   }


Reference:



------------------------------------------------------------------------------------------------------------------------

Giới thiệu

Một s php component s dụng composer đ cài đặt(ging kiu dependency).
Các bạn có th tham kho trang: https://packagist.org/  đ thy, trên đó có rt nhiu các repository, library do developer lập nên.

Lợi ích ca việc dùng composer này là:
  • Khi mình tạo 1 library có phụ thuộc vào library ca người khác, đương nhiên là mình không th nhét tt c library ca họ vào ca mình được, vì như thế chật máy, hơn nữa lại không đm bo là ta luôn có thư viện mới nht đ làm việc.
--> Mình sẽ tạo 1 file khai báo là library ca mình phụ thuộc thằng này thằng kia...
  •  User ch cn 1 lệnh nh là có th down toàn bộ các library ln dependency ca library đó v đ dùng.
  • User cập nhật library cũng d dàng.


Cách làm: 

Đu tiên, hãy tưng tượng bạn project ca bạn cn một s các library bên ngoài, thế thì bạn sẽ phi khai báo là bạn cn nó.

> Bước 1: cài đặt composer, dùng command line: 

curl -s http://getcomposer.org/installer | php

> Bước 2: Vào thư mục gc ca bạn, tạo ra 1 file composer.json, bên trong đó ghi 1 s thông tin cơ bn.
ví dụ:

{
    "name": "your-vendor-name/package-name",
    "description": "A short description of what your package does",
    "require": {
        "php": ">=5.3.0",
        "another-vendor/package": "1.*"
    }
}


Gii thích: ch require chính là những ch đ bạn khai báo các thư viện cn dùng cho project ca bạn.
ví dụ bạn có th vào https://packagist.org/packages/bryglen/yii2-apns-gcm, ta sẽ thy ch:

    require: "bryglen/yii2-apns-gcm": "dev-master"

> Bước 3: Sau khi add các require cn thiết vào, bạn hãy gõ lệnh sau đ composer tự download và cài đặt:

php composer.phar install

> Chú ý: Có 1 s package là trong trạng thái dev chứ không phi stable, nếu như thư viện mà bạn require lại require môt thư viện dạng dev, thì bạn sẽ không th cài đặt được repository. 
Cách đơn gin nht là bạn đặt thêm option sau vào file composer.json:

    "minimum-stability":"dev" 


Cách làm với Yii framework:

Nhiu thư viện yii cũng dùng composer. Với yii thì các thư viện ngoài thường được lưu mặc định trong thư mục protected/extension hoặc protected/vendors. Các bạn cn khai báo thêm chúng trong file composer như sau:

        "config": {
             "vendor-dir": "protected/vendors"
   }


Tham kho: 

Comments

Popular posts from this blog

Setup splunk for virtual machine using Vagrant and Ansible

Splunk is a great tool for collecting and indexing data in any kind(log files, changes, tickets, scripts…) from any sources (sensors, networks, databases, smartphones,..( Then creating index for data which then support us to analyse data or search data in the way we like it. We want to try installing Splunk on Virtual environment in order to test, try, study… And with Vagrant&Ansible we can easily save our virtual machine configuration for next time use(in case my virtual machine was broken). There is already Splunkbox by Phips on githubs for simply installing:  https://github.com/phips/splunkbox 1.Download and install Virtual Box (You need a Virtual machine provider for Vagrant(Virtualbox is prefered)) https://www.virtualbox.org/wiki/Downloads 2. Download and install Vagrant   http://www.vagrantup.com/downloads 3. Download and install Ansible   http://docs.ansible.com/intro_installation.html   For MAC:  https://devopsu.com/guides/ansible-mac-osx.html

Launch android application within Unity app.

In Unity android application(called application A), you want to launch other android app(application B). I had tried to follow some entries and try :  List<ApplicationInfo> packages =  getPackageManager() .getInstalledApplications(PackageManager. GET_META_DATA ); for  (ApplicationInfo packageInfo : packages) { if (packageNameToOpen.equals(packageInfo. packageName )){ try { Intent intent =  new  Intent(); intent.putExtra(key, value); intent.setComponent( new  ComponentName(packageName,activityName)); UnityPlayer. currentActivity .startActivity(intent); } catch (Exception e){ } Android manages its items in "tasks" and "activity stack". If you normally call new activity as above, the activity of other app would be put in to same task of the current applications, which means you are not openning new application. You can see an example of opening mail client, the mail client allow you to call mail activity and it is put in to your curren

Install Ruby on Rails on MAC

Hello, I am quite familliar with Apache's friend products like XAMPP, LAMPP... And this is the first time I try Ruby on Rails(RoR). I'm quite stuck on installing it. As a normal PHP guy, I first try install Rails, following this tutorial: http://installrails.com/ And the next thing I want to learn about is how RoR works with MySQL, PhpMyadmin. So I tried to make it connect to XAMPP's MySQL but it did not work at all. I found out that, using mySQL in XAMPP package is not a common method, what I should do is to install MySQL as a single service onto my MAC. So I did following: 1. Install MYSQL with HomeBrew Did you install HomeBrew, if not, please follow:  https://coolestguidesontheplanet.com/installing-homebrew-on-os-x-el-capitan-10-11-package-manager-for-unix-apps/ Next, type this command to install MySQL to your machine: $ brew install mysql This will install mysql to your computer, if the installation is success, you could try start using mySQL with this c