Monday, August 8, 2016

How to set cookie in controller with Laravel 5

It's not easy to do. I've tried to find on Google, and saw many people had the same question with me.

And here is a good information for you:


use Cookie;
use Illuminate\Http\Request;

class DefaultController extends \App\Http\Controllers\Controller
{
  /**
   * Home page 
   * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
   */
  public function home(Request $request)
  {
    $test = Cookie::make('test_cookie1', 'cookie1 value', 60);

    $response = new \Illuminate\Http\Response(view('frontend.default.home', ['test' => 'send to template']));
    // Set 2 cookie
    $response->withCookie(cookie('test_cookie2', $request->referrer, 45000))
      ->withCookie($test);
    return $response;
  }
}

Test:
set cookie in controller with Laravel 5

Monday, March 14, 2016

Tạo form contact với Laravel

Hướng dẫn tạo Form Contact đơn giản với Laravel 

0. Chuẩn bị
Laravel + Bảng contact + cấu hình kết nối tới database


CREATE TABLE `tld_contact` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `subject` varchar(255) NOT NULL,
  `content` text,
  `is_read` tinyint(1) NOT NULL DEFAULT '0',
  `fullname` varchar(100) NOT NULL COMMENT 'ho ten nguoi gui lien he',
  `email` varchar(255) NOT NULL,
  `phone_number` varchar(15) DEFAULT NULL,
  `address` varchar(15) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  `product_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

1. Tạo controller

php artisan make:controller --plain Frontend\ContactController
Hàm này sẽ giúp tạo ra 1 controller:  app\Http\Controllers\Frontend\ContactController.php
Với đầy đủ các hàm có thể tương tác với 1 đối tượng: index, create, store, edit, show, edit, destroy
Ở đây sẽ dùng đến 2 hàm: create & store để xử lý việc hiển thị form contact và xử lý lưu contact vào DB

Tuesday, February 16, 2016

Laravel Oracle with OCI 8

Kết nối Oracle database với OCI 8

1. Cài đặt package yajra/laravel-oci8
cd /path/to/project-name/
composer require yajra/laravel-oci8
2. Cấu hình bổ sung provider trong config/app.php
Yajra\Oci8\Oci8ServiceProvider::class
3. Publish configuration (chạy nếu cần, ko bắt buộc)
php artisan vendor:publish --tag=oracle
Laravel sẽ sinh 1 file cấu hình riêng cho oracle (config/oracle.php )

4. Configure file database: config/database.php
Thêm vào 'connections' đoạn code sau:
'oracle' => array(  
  'driver' => 'oracle',  
  'host' => '10.58.x.x',  
  'port' => '1521',  
  'database' => 'orcl',  
  'username' => 'laravel',
  'password' => '123456',  
  'charset' => 'AL32UTF8',  
  'prefix' => '',
)
5. Done.

Tham khảo: https://github.com/yajra/laravel-oci8/wiki/Installation

Nếu bạn gặp khó khăn khi enable php_oci8 trên window thì xem ở đây: Enable php_oci8.dll on window

Bài tiếp: Sử dụng cùng 1 lúc 2 kết nối sang Oracle và Mysql