Kamis, 23 Oktober 2014

Membuat PHP project dengan micro framework silex dan ORM propel 2.*

  • Buat folder kosong untuk menyimpan project. misal: myproject
  • Instalasi silex dan propel2 menggunakan composer. buat file composer.json di dalam folder myproject. Berikut codenya :
composer.json
{
    "require": {
        "silex/silex": "~1.2",
        "propel/propel": "~2.0@dev",
        "propel/propel-service-provider": "*"
    },
    "autoload": {
        "classmap": ["conf/generated-classes/"],
        "psr-0": { "bookstore": "conf/generated-classes/" }
      }
}
  • Buka cmd, masuk ke folder myproject, ketikan composer install (Untuk windows). Tunggu sampai selesai
  • Dalam folder myproject, buat folder sebagai berikut :
myproject
- app
- conf
- web
  • lanjut setting propel, dalam folder conf buatlah beberapa file sebagai berikut:
set_path.bat
PATH=%PATH%;d:\projects\localhost\propel-project\vendor\bin
schema.xml
<?xml version="1.0" encoding="UTF-8"?>
<database name="bookstore" defaultIdMethod="native">
  <table name="book" phpName="Book">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="title" type="varchar" size="255" required="true" />
    <column name="isbn" type="varchar" size="24" required="true" phpName="ISBN"/>
    <column name="publisher_id" type="integer" required="true"/>
    <column name="author_id" type="integer" required="true"/>
    <foreign-key foreignTable="publisher" phpName="Publisher" refPhpName="Book">
      <reference local="publisher_id" foreign="id"/>
    </foreign-key>
    <foreign-key foreignTable="author">
      <reference local="author_id" foreign="id"/>
    </foreign-key>
  </table>
  <table name="author" phpName="Author">
    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true"/>
    <column name="first_name" type="varchar" size="128" required="true"/>
    <column name="last_name" type="varchar" size="128" required="true"/>
  </table>
  <table name="publisher" phpName="Publisher">
   <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
   <column name="name" type="varchar" size="128" required="true" />
  </table>
</database>
propel.json
{
    "propel": {
        "database": {
            "connections": {
                "bookstore": {
                    "adapter": "mysql",
                    "classname": "Propel\\Runtime\\Connection\\ConnectionWrapper",
                    "dsn": "mysql:host=localhost;dbname=bookstore",
                    "user": "root",
                    "password": "r00tdb",
                    "attributes": []
                }
            }
        },
        "runtime": {
            "defaultConnection": "bookstore",
            "connections": ["bookstore"]
        },
        "generator": {
            "defaultConnection": "bookstore",
            "connections": ["bookstore"]
        }
    }
}
    buatlah manual folder generated-classes di folder conf dan wamp harus aktif
*catatan : untuk schema.xml dan propel.json bisa dilihat di dokumentasi propel
  • Buat database bookstore
  • Build propel dengan menggunakan langkah sebagai berikut:
    • buka cmd, masuk folder myproject/conf
    • ketikan set_path.bat
    • ketikan propel sql:build
    • ketikan propel sql:insert
    • ketikan propel model:build
    • ketikan propel config:convert
  • Masuk dalam folder myproject/app buat file sebagai berikut:
app.php
<?php

use Symfony\Component\HttpFoundation\Request;

$app = require_once __DIR__ . '/bootstrap.php';

$app['debug'] = true;

$app->get('/', function (Request $request) use ($app) {
    $authors = AuthorQuery::create()->find();
    // $authors contains a collection of Author objects
    // one object for every row of the author table
    foreach($authors as $author) {
      echo $author->getFirstName()."<br/>";
    }
})->bind('homepage');

return $app;
bootstrap.php
<?php

require_once '/../vendor/autoload.php';

error_reporting(E_ERROR);

$app = new Silex\Application();

//Connection to Propel
$app->register(new Propel\Silex\PropelServiceProvider(), array(
    'propel.config_file' => __DIR__.'/../conf/generated-conf/config.php',
    'propel.model_path' => __DIR__.'/../conf/generated-classes'
));

return $app;
  • Masuk dalam folder myproject/web buat file sebagai berikut:
.htaccess
<IfModule mod_rewrite.c>
    Options -MultiViews

    RewriteEngine On
    #RewriteBase /path/to/app
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</IfModule>
index.php
<?php

$app = require_once __DIR__ . '/../app/app.php';
$app->run();
    insert data ke tabel yang sudah di buat misal: di phpmyadmin
Oke semua langkah sudah oke, jangan lupa tambahkan vhost apache nya ke myproject/web
    lalu panggil index.php bila terjadi error composer harus di update

Kalo masih ga paham, langsung datangan weh jelemana...

1 komentar:

  1. pas mau build propel.
    error propel is not recognized as an internal or external command

    BalasHapus