Commit 796b6192fec63c720b54c2f3a0666e07f3207553
Merge pull request #7 from mydevcommunity/tips-composer
Create pengenalan-composer.mdk4ml committed on 2/6/2015, 9:25:32 AM
Parent: 17cd196cdb2128c2e176498189630eab8a0e4d06
Parent: 48ac828d202c9646dc8566ff0fd3e00760b1c5cf
Files changed
_tips/pengenalan-composer.md | added |
_tips/pengenalan-composer.md | ||
---|---|---|
@@ -1,0 +1,76 @@ | ||
1 | +--- | |
2 | +layout: tips | |
3 | +title: PHP: Pengenalan kepada Composer | |
4 | +author: kamalmustafa | |
5 | +date: 2015-02-05 | |
6 | +level: 2 | |
7 | +summary: > | |
8 | + Penerangan ringkas tentang penggunaan Composer dalam aplikasi PHP. | |
9 | +--- | |
10 | + | |
11 | +Composer adalah tools untuk mendapatkan *dependencies* bagi projek PHP anda. Sebelum Composer, jika anda ingin menggunakan sebarang *library* luar, anda perlu download ia terlebih dahulu, unzip (jika dalam bentuk archive), dan kemudian *copy* ke folder projek anda. Bagaimana pula jika *library* tersebut turut bergantung kepada *library* lain ? Ulang semula proses sebelum ini. | |
12 | + | |
13 | +Terlebih dahulu anda perlu install `composer`. Ia boleh dilakukan melalui *command* berikut:- | |
14 | + | |
15 | +```console | |
16 | +curl -sS https://getcomposer.org/installer | php | |
17 | +``` | |
18 | +Anda akan mendapat *output* seperti berikut:- | |
19 | + | |
20 | +```console | |
21 | +#!/usr/bin/env php | |
22 | +All settings correct for using Composer | |
23 | +Downloading... | |
24 | +Composer successfully installed to: /home/kamal/php/myshop/composer.phar | |
25 | +Use it: php composer.phar | |
26 | +``` | |
27 | +Seterusnya, untuk men'download' *library* yang anda inginkan, bina fail bernama `composer.json` seperti berikut:- | |
28 | + | |
29 | +```json | |
30 | +{ | |
31 | + "require": { | |
32 | + "swiftmailer/swiftmailer": "5.3.1" | |
33 | + } | |
34 | +} | |
35 | +``` | |
36 | + | |
37 | +Kemudian, jalankan arahan berikut:- | |
38 | + | |
39 | +```console | |
40 | +php composer.phar install | |
41 | +``` | |
42 | +Anda akan mendapat output lebih kurang seperti berikut apabila selesai:- | |
43 | + | |
44 | +<a href="http://imgur.com/tVjCE09"><img src="http://i.imgur.com/tVjCE09.png" title="source: imgur.com" /></a> | |
45 | + | |
46 | +<div class="admonition-info"> | |
47 | + Perhatikan satu folder baru bernama <code>vendor</code> dibina. Ia akan mengandungi kesemua code yang di'download' melalui <code>composer</code>. | |
48 | +</div> | |
49 | +<div> </div> | |
50 | + | |
51 | +Setelah `composer` selesai download, anda boleh mula menggunakan library tersebut dalam *script* PHP anda dengan hanya menambah baris berikut:- | |
52 | + | |
53 | +```php | |
54 | + | |
55 | +require 'vendor/autoload.php'; | |
56 | +``` | |
57 | + | |
58 | +Contohnya, bina fail baru bernama `mail.php` seperti berikut:- | |
59 | + | |
60 | +```php | |
61 | + | |
62 | +require 'vendor/autoload.php'; | |
63 | + | |
64 | +$transport = Swift_SmtpTransport::newInstance('127.0.0.1', 25); | |
65 | +$mailer = Swift_Mailer::newInstance($transport); | |
66 | + | |
67 | +$message = Swift_Message::newInstance('Wonderful Subject') | |
68 | + ->setFrom(array('john@doe.com' => 'John Doe')) | |
69 | + ->setTo(array('kamal-test@mailinator.com')) | |
70 | + ->setBody('Here is the message itself') | |
71 | + ; | |
72 | + | |
73 | +$result = $mailer->send($message); | |
74 | +``` | |
75 | + | |
76 | +Dan anda boleh jalankannya melalui `php mail.php` dan email akan dihantar. |
Built with git-ssb-web