Tag Archive คอนฟิ

สร้าง muti site ใน laravel

ต้องการสร้างเว็บรองรับหลายๆ บริษัท โดยที่ใช้ code ชุดเดียวกันและให้ฝ่ายที่ดูแลสามารถเพิ่มข้อมูลและเว็บไซต์ ได้เองโดยที่ไม่ต้องใช้ความรู้ทางด้านการเขียนโปรแกรมเลย

เจอ package ชื่อ delatbabel/site-config แต่เพราะว่าเค้ารองรับแค่ laravel 5.1 เลยต้องสร้าง project ใหม่ตั้งแต่ต้นเลย

  1. พิมพ์ command[code language=”text” title=”installation 1=5.1″]composer create-project laravel/laravel sites "5.1.*"[/code]
  2. แก้ config ในไฟล์ .env ให้ติดต่อ databse ได้[code language=”text” title=”.env”]…
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_DATABASE=sites
    DB_USERNAME=root
    DB_PASSWORD=xxx
    …[/code]
  3. เพิ่ม package ในไฟล์ composer.json[code language=”php” title=”composer.json”]…
    "require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",

    "delatbabel/site-config": "~1.0"
    },
    …[/code]
  4. run command[code language=”text” title=”command”]
    composer update
    [/code]
  5. เปิดไฟล์ config\app.php เพิ่ม[code language=”php” title=”configpp.php”]

    ‘providers’ => [

    Delatbabel\SiteConfig\SiteConfigServiceProvider::class,

    ],

    ‘aliases’ => [

    ‘SiteConfigSaver’ => Delatbabel\SiteConfig\Facades\SiteConfigSaver::class,

    ],
    …[/code]

  6. เพิ่ม Boostrap the Config Loader ใน 2 ไฟล์ คือ \app\Console\Kernel.php และ \app\Http\Kernel.php ต้องครบ 2 ไฟล์นะครับ ผมใส่ไม่ครบงงอยู่นานมากทำไม่มันมันใช้ไม่ได้[code language=”php” title=”\app\Http\Kernel.php”]

    protected function bootstrappers()
    {
    $bootstrappers = parent::bootstrappers();

    // Add the SiteConfig bootstrapper to the end.
    $bootstrappers[] = ‘Delatbabel\SiteConfig\Bootstrap\LoadConfiguration’;

    return $bootstrappers;
    }
    …[/code]

  7. Incorporate and Run the Migrations โดย run command[code language=”text” title=”command”]
    php artisan vendor:publish –tag=migrations –force
    php artisan migrate[/code]

การติดตั้งเสร็จแล้ว ที่นี้จะทดลองใช้งาน

  1. สร้างไฟล์ \app\Http\Controllers\HomeController.php[code language=”php” title=”\app\Http\Controllers\HomeController.php”]
    <?php

    namespace App\Http\Controllers;

    use App\User;
    use App\Http\Controllers\Controller;

    class HomeController extends Controller {

    /**
    * Show the profile for the given user.
    */
    public function index()
    {

    return ‘title = ‘.config(‘config.title’);

    $config = config()->all();

    return ‘<pre>’. print_r($config ,true).'</pre>’;
    }

    }
    [/code]

  2. เพิ่ม route ในไฟล์ \app\Http\routes.php [code language=”php”]

    Route::get(‘/’, ‘[email protected]’);

    [/code]
  3. ทดลอง set domain ขึ้นมาเทส เปิดไฟล์ C:\Windows\System32\drivers\etc\hosts [code language=”php” title=”host”]

    127.0.0.1 phunsanit.com
    127.0.0.1 pitt.com

    [/code]
  4. เพิ่มข้อมูลทกสอบโดยใช้ [code language=”sql” title=”sql”]
    INSERT INTO `configs` (`id`, `website_id`, `environment`, `group`, `key`, `value`, `type`, `created_at`, `updated_at`) VALUES
    (1, 1, NULL, ‘config’, ‘title’, ‘localhost’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (2, 1, ‘prod’, ‘config’, ‘title’, ‘localhost ON production’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (3, 1, ‘test’, ‘config’, ‘title’, ‘localhost IN TEST SERVER’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (4, 2, NULL, ‘config’, ‘title’, ‘pitt.com’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (5, 2, ‘prod’, ‘config’, ‘title’, ‘pitt.com ON production’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’),
    (6, 2, ‘test’, ‘config’, ‘title’, ‘pitt.com IN TEST SERVER’, ”, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’);

    INSERT INTO `websites` (`id`, `name`, `http_host`, `environment`, `created_by`, `updated_by`, `created_at`, `updated_at`, `deleted_at`) VALUES
    (1, ‘prototype’, ‘localhost’, ‘test’, NULL, NULL, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’, NULL),
    (2, ‘prototype’, ‘pitt.com’, ‘test’, NULL, NULL, ‘0000-00-00 00:00:00’, ‘0000-00-00 00:00:00’, NULL);
    [/code]

  5. ทดสอบโดยเปิด http://localhost/mcms/public/ และ http://pitt.com/mcms/public/

นำไปดัดแปลงให้เหมาะกับงานที่ทำครับ