顯示具有 Laravel 標籤的文章。 顯示所有文章
顯示具有 Laravel 標籤的文章。 顯示所有文章

2019年8月30日 星期五

apache mod rewrite .htaccess

IT邦的說明:https://ithelp.ithome.com.tw/questions/10190295
modRewire說明:https://blog.hinablue.me/apache-note-about-some-rewrite-note-2011-05/
alias 說明:http://phorum.study-area.org/index.php?topic=21235.0

同一個IP不同目錄專案顯示改寫:

apache  centos 7

1.cmd 下開啟編輯設定檔案
sudo vi /etc/httpd/conf/httpd.conf

2.編輯檔案
Alias /shop55 "/var/www/html/shop55/public"

AllowOverride all
RewriteEngine On
#不顯示目錄結構
Options FollowSymLinks

Alias /white55 "/var/www/html/white55/public"

AllowOverride all
RewriteEngine On
#不顯示目錄結構
Options FollowSymLinks



3.在對應的laravel public資料夾底下設定 .htaccess


第一個 ..htaccess


Options -MultiViews

RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /shop55


第二個 .htaccess


Options -MultiViews

RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /white55



重新啟動:
sudo systemctl restart httpd.service


另外一種設定方法

1.cmd 下開啟編輯設定檔案
sudo vi /etc/httpd/conf/httpd.conf

<VirtualHost *:80> DocumentRoot /var/www/html/white55/public ServerName white55.xxxxx.tw</VirtualHost><VirtualHost *:80> DocumentRoot /var/www/html/shop55/public ServerName shop55.xxxxx.tw</VirtualHost>


然後在DNS代管設定,填入A 紀錄  主機名稱別名:shop55   IP:對應IP,等待生效。

就可以使用

shop55.xxxxxx.tw
white55.xxxxx.tw

進入。

2019年8月26日 星期一

Laravel log / storage could not be opened: failed to open stream

參考:https://sjkou.net/2016/07/30/how-to-install-laravel5-on-centos7/

除了資料夾權限是設定成 chmod 777 的指令之外:

chmod -R 777 storage

導致此問題發生的原因還有  SELinux


SELinux導致

觀察主機目前是否有啟動 selinux (CentOS 預設是有啟動 selinux)
getenforce 


暫時性的關掉或開啟 selinux
setenforce 0

開啟
setenforce 1

以下步驟請都做:

(一)
因為有使用 selinux 而你新增了一些檔案或目錄到「網頁目錄 /var/www/html」,明明檔案就存在,卻無法正常從瀏覽器觀看,這不是 file permission chmod 的問題,是 selinux 檔案 http 存取權限的問題,你只需要用 使用SELinux允許使用此命令寫入文件

在對應資料夾下使用此指令
sudo chcon -R -t httpd_sys_rw_content_t 我的專案名稱/storage



(二)
對網站加入 apache 所有權,在 Laravel 網站的根目錄下執行:
sudo chown -R apache:apache *  
<-FTP上傳檔案可能會有使用者權限問題  
(三)
CentOS 對內部檔案的權限管理非常嚴謹,當其他使用者在看網頁時,CentOS 內其實是有一名叫做 apache 的使用者在瀏覽您的網頁目錄,雖然如此但 apache 也不是什麼檔案都能看,僅能觀看httpd 相關權限的檔案而已,在還沒修改網站權限時你可以透過 ls -Z 來取得目前的權限。
但是 httpd_sys_content_t 僅是瀏覽的權限而已,依據 laravel 的安裝說明bootstrap/cache 及 storage 這兩個資料夾必須要有寫入的權限,所以需再做以下修改:

sudo chcon -Rv -t httpd_sys_rw_content_t 網站/storage 
sudo chcon -Rv -t httpd_sys_rw_content_t 網站/bootstrap/cache 
sudo chmod -R 777 網站/storage 
sudo chmod -R 777 網站/bootstrap/cache


2019年8月23日 星期五

gcp centos7 laravel

說明:https://www.brilliantcode.net/170/centos-7-install-apache-httpd/


第一次設定root帳號需要打入密碼

sudo passwd root

安裝 EPEL

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

1.安裝Apache and http模組

sudo yum install httpd mod_ssl openssl

啟動Apache,並且設定為開機自動啟動

sudo systemctl start httpd
sudo systemctl enable httpd


確認Apache執行權限
sudo vim /etc/httpd/conf/httpd.conf

更改
User apache
Group apache
(因為會有FTP檔案上傳問題,可以改成自己的user )

設定或修改Apache目錄

sudo vim /etc/httpd/conf/httpd.conf

顯示:
DocumentRoot "/var/www/html"



讓Apache可以支援.htaccess,請加入AllowOverride All


<directory "/var/www/html">
    AllowOverride All
</Directory>


存檔離開
:wq

重啟APACHE
sudo systemctl start httpd

Selinux 安全性說明
https://www.brilliantcode.net/145/centos-7-check-selinux-status-enabled-or-not/

暫時停止SELinux
sudo setenforce 0

暫時啟動SELinux
sudo setenforce 1

sudo vim /etc/selinux/config

永久停止SELinux:將第6行改為SELINUX=disable
永久啟用SELinux:將第6行改為SELINUX=enable

開啟防火牆,讓 web (port:80, 443) 流量可以穿過 firewall

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

重啟Apache服務
sudo systemctl restart httpd.service

2. install mysqld

sudo yum -y install mariadb-server

安全性服務+設定密碼
mysql_secure_installation

3.PHP 安裝 7.2

yum -y install mod_php72w php72w-cli php72w-common php72w-gd php72w-imap php72w-ldap php72w-mbstring php72w-mysql php72w-pdo php72w-pear php72w-xml php72w-xmlrpc

重啟
systemctl restart httpd.service

4.安裝 Composer

複製安裝文件 PHP 檔案
執行安裝 composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

 php composer-setup.php

完成以後,我們要放到全域環境,這樣才能讓任何專案無須從新安裝 composer。記得 Composer 限制不可在最高權限 root 中執行。

mv composer.phar /usr/local/bin/composer

5.安裝 phpMyAdmin

.用 composer 下載 (建議,因為版本最新,解決許多問題)

chmod 777 /var/www/html  <-- 提供寫入權限給 composer 建立路徑

cd /var/www/html
composer create-project phpmyadmin/phpmyadmin

cp -i /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config.inc.php

GCP登入方式

http://zxcvbnm2749.blogspot.com/2018/07/ssh-ftp-google-cloud.html

6.git安裝

sudo rpm -U https://centos7.iuscommunity.org/ius-release.rpm sudo yum install git2u
sudo yum install git2u

7.權限問題


在對應資料夾下使用此指令
chcon -R -t httpd_sys_rw_content_t 我的專案名稱/storage


CentOS 對內部檔案的權限管理非常嚴謹,當其他使用者在看網頁時,CentOS 內其實是有一名叫做 apache 的使用者在瀏覽您的網頁目錄,雖然如此但 apache 也不是什麼檔案都能看,僅能觀看httpd 相關權限的檔案而已,在還沒修改網站權限時你可以透過 ls -Z 來取得目前的權限。


但是 httpd_sys_content_t 僅是瀏覽的權限而已,依據 laravel 的安裝說明bootstrap/cache 及 storage 這兩個資料夾必須要有寫入的權限,所以需再做以下修改:




1
2
3
4
$ chcon -Rv -t httpd_sys_rw_content_t blog/storage
$ chcon -Rv -t httpd_sys_rw_content_t blog/bootstrap/cache
$ sudo chmod -R 777 blog/storage
$ sudo chmod -R 777 blog/bootstrap/cache


2018年8月29日 星期三

laravel voyager 教學

說明:https://github.com/the-control-group/voyager

中文化這邊有:https://laravel-china.org/topics/4720/i-also-translated-laravels-background-extension-voyager


1.首先安裝laravel 5.5

composer create-project laravel/laravel 5.5

composer update

2.安裝voyager

composer require tcg/voyager

3.設定env
APP_URL=http://localhost:8000
DB_DATABASE=xxxx
DB_USERNAME=xxxx
DB_PASSWORD=xxxx

4.執行voyager install migrate

php artisan voyager:install

PS:如果出現錯誤可以到config/databases 調整mysql

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],


3.創建admin

php artisan voyager:admin your@email.com --create


5.網址打入 localhost:8000/admin 使用剛剛輸入的密碼登入


6.設定可以使用google analytics功能

說明:https://www.youtube.com/watch?v=Qw5HQiZIehg

在setting頁面->admin->

Google Analytics Client ID (used for admin dashboard) setting('admin.google_analytics_client_id')

填入https://console.developers.google.com/apis/dashboard
設定的 oauth 憑證的client_id。

設定完畢即可以在disable登入gmail帳號,並看到相關數據資料。




2018年8月15日 星期三

Laravel hide whoops error

在設定Laravel錯誤時候,
會設定DEBUG方便偵錯,

APP_ENV=local
APP_DEBUG=true

但是DEBUG時候再Woops錯誤時候會顯示ENV DB帳密等資訊,

可以藉由調整Laravel底下的 config/app.php來避免顯示過多的
調整以下限制會在Whoops輸出錯誤資訊時候,將對應的帳密等ENV設定顯示****

1.全部阻擋
'debug_blacklist' => [
        // '_COOKIE' => array_keys($_COOKIE),
        '_SERVER' => array_keys($_SERVER),
        '_ENV' => array_keys($_ENV),        
    ],
2.阻擋顯示指定的參數
'debug_blacklist' => [
        '_ENV' => [
            'APP_KEY',
            'DB_PASSWORD',
            'REDIS_PASSWORD',
            'MAIL_PASSWORD',
            'PUSHER_APP_KEY',
            'PUSHER_APP_SECRET',
        ],
        '_SERVER' => [
            'APP_KEY',
            'DB_PASSWORD',
            'REDIS_PASSWORD',
            'MAIL_PASSWORD',
            'PUSHER_APP_KEY',
            'PUSHER_APP_SECRET',
        ],
        '_POST' => [
            'password',
        ],
    ], 

2018年8月13日 星期一

ckeditor + ckfinder 應用在Laravel




一.ckeditor提供編輯器功能

在view的頁面加載editor  full js

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">

<!-- 可选的Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

<script src="//cdn.ckeditor.com/4.10.0/full/ckeditor.js"></script>
<textarea id="editor1" name="editor1" cols="100" rows="30" name="update_context">

</textarea>
<script >

var editor = CKEDITOR.replace( 'editor1' );
CKFinder.setupCKEditor( editor );
</script>

二.ckfinder 
1.Laravel  下安裝 ,版本需要大於5.5 且需要extension gd等
 
composer require ckfinder/ckfinder-laravel-package
2.Run the command to download the CKFinder code.
 
php artisan ckfinder:download

3.Publish the CKFinder connector configuration and assets.
 
php artisan vendor:publish --tag=ckfinder

4.Create a directory for CKFinder files and allow for write access to it. By default CKFinder expects the files to be placed in public/userfiles (this can be altered in the configuration).
 
mkdir -m 777 public/userfiles
5.laravel config 會多出一個設定檔案
 
config/ckfinder.php
6.在view頁面引入
 
@include('ckfinder::setup')
7.上傳的圖片位置會出現在 userfiles 資料夾底下。

8.程式碼範例
 

<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">

<!-- Bootstrap主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">

<!-- jQuery文件。在bootstrap.min.js 之前引入 -->
<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

<script src="//cdn.ckeditor.com/4.10.0/full/ckeditor.js"></script>
@include('ckfinder::setup')
<textarea id="editor1" name="editor1" cols="100" rows="30" name="update_context">

</textarea>
<script >

var editor = CKEDITOR.replace( 'editor1' );
CKFinder.setupCKEditor( editor );
</script>


9.資料夾權限等級劃分
 (不是用laravel session  因為laravel session無法在config設定檔用)

在登入的controller控制器使用php session

//登入
Session::put('userDatas',$account);
$_SESSION['ckfinder_file'] = Session::get('userDatas');

//登出
session_destroy();

在  config/ckfinder.php 加入以下片段

//最前面
session_start();

if(isset($_SESSION['ckfinder_file'])) {

    $ckfinder_file = $_SESSION['ckfinder_file'];
} else {
    $ckfinder_file = "temp";
}

// Backend 調整

$config['backends']['default'] = array(

    'name'         => 'default',
    'adapter'      => 'local',
    'baseUrl'      => env('APP_URL').'userfiles/'.$ckfinder_file."/",
    'root'         => public_path('userfiles/').$ckfinder_file."/",
    'chmodFiles'   => 0777,
    'chmodFolders' => 0755,
    'filesystemEncoding' => 'UTF-8'

);

2018年8月12日 星期日

Laravel apache alias mod Rewrite 設定

有一個固定IP,但在資料夾底下有多個專案,需要重新改向路徑。

1.httpd.conf設定

 
LoadModule rewrite_module modules/mod_rewrite.so

需要拿掉註解#啟用
2.在httpd.conf 增加對應資料夾別名
 

Alias /shop55 "C:/website/www/laravel55/shop55/public"
<Directory "C:/website/www/laravel55/shop55/public">
</Directory>

Alias /white55 "C:/website/www/laravel55/white55/public"
<Directory "C:/website/www/laravel55/white55/public">
</Directory>


3.在對應的專案資料夾public下需要新增 .htaccess檔案 並寫入RewriteBase /Alias name

舉例:white55下的 C:/website/www/laravel55/white55/public

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /white55
</IfModule>

4.重啟apache

 
c:\Apache24\bin\httpd -k restart

5.額外問題,用main當路徑會有導向失敗的問題?,用mainIndex就沒問題?

2018年8月11日 星期六

jQuery File Upload 檔案上傳套件 應用在laravel 紀錄

說明: https://blueimp.github.io/jQuery-File-Upload/

在Laravel框架下,因為MVC分開原因,需要做以下設定。

1.View: 引入Js,跟html語法。
  	

jQuery File Upload Demo

Basic Plus UI version


File Upload widget with multiple file selection, drag&drop support, progress bars, validation and preview images, audio and video for jQuery.
Supports cross-domain, chunked and resumable file uploads and client-side image resizing.
Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads.


Add files...
 

Demo Notes

  • The maximum file size for uploads in this demo is 999 KB (default file size is unlimited).
  • Only image files (JPG, GIF, PNG) are allowed in this demo (by default there is no file type restriction).
  • Uploaded files will be deleted automatically after 5 minutes or less (demo files are stored in memory).
  • You can drag & drop files from your desktop on this webpage (see Browser support).
  • Please refer to the project website and documentation for more information.
  • Built with the Bootstrap CSS framework and Icons from Glyphicons.


2.在route需要設定any方式  (讓post,get,delete.都可以通過)

Route::any('/file','HomeController@file');



3.需要加載PHP UploadHandler class,在Laravel裡面可以用composer加載
EX:放在white/tool工具資料夾,將UploadHandler檔案加入namespace,用composer dump-autoload 更新加載。
UploadHandler檔案需要做以下調整:


namespace white\tool;

class UploadHandler

{

  //public function __construct 裡面的script_url調整位置

  'script_url' => 'https://myurl/laravel55/shop55/public/file',

}
4.在加載的js裡面有一個檔案,main.js需要調整 Url設定,用來指向route設定的方法
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        url:'https://myurl/laravel55/shop55/public/file'
    });



5.調整php.ini設定
extension=php_gd2.dll
6.錯誤說明:
-如果跳出$等jquery錯誤,可能是沒有先加入jquery的function
-刪除檔案跳出405 not method allow,可能是route沒有設定正確 (any方法,確定route對),或參考3.4路徑確定正確
-上傳跳出Filed to resize image,需要調整5 php.ini設定
-記得檔案資料夾權限要設定允許寫入跟讀取

2018年5月23日 星期三

Laravel呼叫方法

說明:https://stackoverflow.com/questions/28573860/laravel-requestall-should-not-be-called-statically/28574081

輔助方法:https://docs.laravel-dojo.com/laravel/5.2/helpers

1. static方法
use Request;

public static function test(Request $request)
{ 
    $data= Request::all();
}


2.  一般方法
use Illuminate\Http\Request;

public static function test(Request $request)
{
   $age = \Request::input('age');
   $data = $request->all();
}



3.輔助方法

use Illuminate\Http\Request;

public static function test(Request $request)
{
    $data= request()->all();
}



2018年5月10日 星期四

laravel class not found 問題

在laravel5 不管新增加什麼控制器/model 有用到classmap
請先打composer dump-autoload

避免出現class not found

2018年5月6日 星期日

Larvel4.2 升級到5.0 5.2一些備註

larvel 升級的說明:


(一) 4.2升級到5.0

https://laravel.tw/docs/5.0/upgrade

大升級有幾個操作上文件沒有說到的部份要注意:

1.執行composer update時候出錯,

需要將vendor/compiled.php 移除,在重新執行composer update

2.注意控制器的命名空間問題

將所有的控制器移到 app/Http/Controllers 目錄下。既然在本指南中我們不打算整合到完整的命名空間,請將 app/Http/Controllers 添加到 composer.json 的 classmap,接下來,您可以從 app/Http/Controllers/Controller.php 基底抽象類別中移除命名空間,並確認整合過來的控制器是繼承這個基底類別。
在 app/Providers/RouteServiceProvider.php 檔案中,將 namespace 屬性設定為 null

3.注意CSRF問題

預設情況下,所有路由都會使用 CSRF 保護。若想關閉它們,或是只在特定路由開啟,請移除 App\Http\Kernel 中 middleware 陣列內的這一行:
'App\Http\Middleware\VerifyCsrfToken',

4.移除Form 跟 Html功能 要使用需要以下調整:

(1)在composer.json  裡面

 require 追加
        "laravelcollective/html": "~5.0"

使用composer update 更新

(2) config/app.php檔案裡面調整

 'providers' 追加     
        'Collective\Html\HtmlServiceProvider',

'aliases' 追加
        'Form' => 'Collective\Html\FormFacade',
        'Html' => 'Collective\Html\HtmlFacade',


(二) 4.2升級到5.2

https://laravel.tw/docs/5.0/upgrade

1.注意Input功能消失,要使用需要在 config/app.php檔案裡面調整:


 This commit removed Input facade definition from config/app.phphence you have to manually add that in to aliases array as below,
'Input' => Illuminate\Support\Facades\Input::class,
Or You can import Input facade directly as required,
use Illuminate\Support\Facades\Input;














2018年5月1日 星期二

Laravel controller,model更改目錄

laravel 4底下
預設controller目錄 app/controller
預設model目錄 app/model

如果要在兩個目錄底下在放資料夾,在資料夾底下放對應的controller,model 做法如下: 

app/controller/player 
app/model/player 

修改composer.json 檔案
在autoload追加classmap
 

"autoload": {
  "classmap": [
   "app/commands",
   "app/controllers",
   "app/controllers/player",
   "app/models",
   "app/models/player",
   "app/database/migrations",
   "app/database/seeds",
   "app/tests/TestCase.php"
  ]
 },



 調整完畢後,使用指令
composer dump-autoload

 原因:
 因為 database 文件夾使用 classmap 來做加載的。所以只有在打了composer dump-autoload之後  composer 才會更新 autoload_classmap 的內容。

2018年2月17日 星期六

laravel homestead

 參考教學:
https://laravel.tw/docs/4.2/homestead
https://blog.wu-boy.com/2014/09/get-homestead-vagrant-vm-running/

一. 安裝
1. Vagrant Installer
https://www.vagrantup.com/downloads.html
2. Virtual Box
https://www.virtualbox.org/wiki/Downloads
3. git tool
http://git-scm.com/downloads

二. 下指令下載vagrant box 的laravel/homestead
$ vagrant box add laravel/homestead

三. 安裝homestead
$ git clone https://github.com/laravel/homestead.git Homestead

四. 打開.Homestead資料夾,底下的Homestead.yaml 設定對應

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: C:\Users\zxcvbnm2749\Code
      to: /home/vagrant/Code

sites:
    - map: homesteadwhite
      to: /home/vagrant/Code/public
databases:
    - homestead

五. 設定電腦host的對應網址

http://192.168.10.10/     homesteadwhite

六. 啟動vagrant up

cd目錄到homestead資料夾,執行指令,就可以成功啟動機器
vagrant up

七. vagrant 指令

vagrant 開機
vagrant up
vagrant 關機
vagrant halt
vagrant ssh登入
vagrant ssh

八. 連入資料庫方法
因為vagrant預設連入DB是使用內建的mysql:homestead資料庫
DB:localhost
port:33060
帳號:homestead
密碼:secret
設定在laravel的 app\config\local\database.php
如果要更改成自己本機的mysql需要調整此設定檔
   'mysql' => array(
   'driver'    => 'mysql',
   'host'      => '59.127.xxx.xxx',
   'database'  => 'white',
   'username'  => 'xxxx',
   'password'  => 'xxxx',
   'charset'   => 'utf8',
   'collation' => 'utf8_unicode_ci',
   'prefix'    => '',
  ),














2016年9月5日 星期一

Laravel Session

Laravel 本身即有自己的session,非使用PHP 原生的session。 

1.
一般PHP使用規格上會有以上作法
在開頭先使用session_start() ,之後才能使用session語法

if (session_status() == PHP_SESSION_NONE) {    
     session_start();
 }

 $_SESSION['account'] = "white" ; 

 


在Laravel語法中,
如在Controller想要使用以上的$_SESSION['account'] ,
也需要在開頭class就先 session_start();

Laravel也提供了自己的session工具, Laravel的session 不等於PHP的session,
以下是Laravel 常用的session的class。

Laravel session說明:Laravel 4.2 Session說明文件

session 的設定檔配置在 app/config/session.php 中

儲存項目到 Session 中
Session::put('key', 'value');

儲存項目進 Session 陣列值中

Session::push('user.teams', 'developers');

從 Session 取回項目

$value = Session::get('key');

從 Session 取回項目,若無則回傳預設值

$value = Session::get('key', 'default');

$value = Session::get('key', function() { return 'default'; });

從 Session 取回項目,並刪除

$value = Session::pull('key', 'default');

從 Session 取出所有項目

$data = Session::all();

判斷項目在 Session 中是否存在

if (Session::has('users'))
{
    //
}

移除 Session 中指定的項目

Session::forget('key');

清空整個 Session

Session::flush();

重新產生 Session ID

Session::regenerate();