win7+php7+laravel5.3+nginx+oracle12c支持 全教程
首先win7 環(huán)境是必須的,這里就不說如何安裝OS了,請自行百度
1. 安裝 phpStudy,這是我第一次聽說這個軟件,看起來真的不錯,可以自行選擇版本將運行環(huán)境進行打包處理,這里直接選擇php 7.0.12-nts+nginx即可
2. 開始安裝composer ?,下載地址是 ?https://getcomposer.org/Composer-Setup.exe,一路確定即可
3.因為composer源的問題可以直接換源,執(zhí)行以下命令
??composer?config?-g?repo.packagist?composer?https://packagist.phpcomposer.com
4. 下載laravel5.3 版本,下載地址是:https://github.com/laravel/laravel ? 并將其加壓縮到你需要的位置上即可,我是 D:\runninglaravel
5. 進行composer安裝 ,使用cmd 進入到?D:\runninglaravel 目錄下,執(zhí)行命令:
?composer?install
6. ?復(fù)制.env.example 為.env文件,執(zhí)行命令
copy?.env.example?.env
7. ?執(zhí)行命令:
?php?artisan?key:generate
8. ?接下來是進行php study的修改,將站點目錄修改為?D:\runninglarave\public
9. 修改nginx.conf 其目的是將所有的請求定位到publicindex.php文件上進行統(tǒng)一處理
??location?/?{ ??????????????root?"D:/running/laravel/public"; ????????????index??index.php; ????????????if?(!-e?$request_filename)?{? ????????????rewrite?^(.*)$?/index.php?s=$1?last;? ????????????break;? ????????????} ????????}
這是我的處理文件,很糙,但是目前可用
10. 到這里為止,再檢查一下php的擴展 ?需要打開的是open_ssl (這個我遇到過問題,即使打開了也會報錯 encry**函數(shù)未定義,我的處理方式是重啟電腦)
11. laravel已經(jīng)安裝好,現(xiàn)在需要做的是進行l(wèi)aravel的oracle支持,laravel默認(rèn)是不支持oracle的,現(xiàn)在添加第三方的組件
? ??? ?composer require yajra/laravel-oci8
12. 如果要用php連接oracle不光需要php擴展,還需要oracle的客戶端程序,
? 下載文件:http://download.oracle.com/otn/nt/instantclient/121020/instantclient-basic-nt-12.1.0.2.0.zip?AuthParam=1488258321_c18d9fdc85acf4d32185f80bcd7e3b5a
? ? 同時還需要將文件解壓縮后,并將解壓縮位置添加在環(huán)境變量上
13. 開啟php ?擴展, 打開php_oci ?php_oci_11g ? php_pdo_oci,需要注意的還有就是打開 ?
oci8.privileged_connect?=?On
? ? ?允許oci_connect連接,這樣就同時支持了兩種方式一個是pdo方式,第二個是開啟oci_connnect連接方式
14. 雖然框架中已經(jīng)有了對oracle的支持,但是在接入點還需要進行修改
? ??在 ?IlluminateDatabaseConnectorsConnectionFactory.php ; 添加兩個類的聲明
use?YajraOci8ConnectorsOracleConnector; use?YajraOci8Oci8Connection;
? ? 還是這個文件還需要重寫兩個函數(shù)
????protected?function?createConnection($driver,?$connection,?$database,?$prefix?=?'',?array?$config?=?[]) ????{ ????????if?($resolver?=?Connection::getResolver($driver))?{ ????????????return?$resolver($connection,?$database,?$prefix,?$config); ????????} ????????switch?($driver)?{ ????????????case?'mysql': ????????????????return?new?MySqlConnection($connection,?$database,?$prefix,?$config); ????????????case?'pgsql': ????????????????return?new?PostgresConnection($connection,?$database,?$prefix,?$config); ????????????case?'sqlite': ????????????????return?new?SQLiteConnection($connection,?$database,?$prefix,?$config); ????????????case?'sqlsrv': ????????????????return?new?SqlServerConnection($connection,?$database,?$prefix,?$config); ????????????case?'oracle': ????????????????return?new?Oci8Connection($connection,?$database,?$prefix,?$config);; ????????} ????????throw?new?InvalidArgumentException("Unsupported?driver?[$driver]"); ????} } public?function?createConnector(array?$config) ????{ ????????if?(!?isset($config['driver']))?{ ????????????throw?new?InvalidArgumentException('A?driver?must?be?specified.'); ????????} ????????if?($this->container->bound($key?=?"db.connector.{$config['driver']}"))?{ ????????????return?$this->container->make($key); ????????} ????????switch?($config['driver'])?{ ????????????case?'mysql': ????????????????return?new?MySqlConnector; ????????????case?'pgsql': ????????????????return?new?PostgresConnector; ????????????case?'sqlite': ????????????????return?new?SQLiteConnector; ????????????case?'sqlsrv': ????????????????return?new?SqlServerConnector; ????????????case?'oracle': ???????????????return?new?OracleConnector; ????????} ????????throw?new?InvalidArgumentException("Unsupported?driver?[{$config['driver']}]"); ????}
最后就是修改config中database.php文件中的配置了, ? ?
??'oracle'?=>?[ ????????????'driver'????????=>?'oracle', ????????????'tns'???????????=>?env('DB_TNS',?''), ????????????'host'??????????=>?'192.168.11.111', ????????????'port'??????????=>?1521, ????????????'database'??????=>?'ORCL', ????????????'username'??????=>?"c##test1", ????????????'password'??????=>?'111111', ????????????'charset'???????=>?env('DB_CHARSET',?'AL32UTF8'), ????????????'prefix'????????=>?env('DB_PREFIX',?''), ????????????'prefix_schema'?=>?env('DB_SCHEMA_PREFIX',?''), ????????],
同時修改default ?
????'default'?=>?'oracle',
至于用戶名是C##開始的,請自行百度,關(guān)于CDB和PDB的區(qū)別
測試代碼:
Route::get('test',?function?()?{ $m?=?DB::table("users_data")->get(); var_dump($m->all()); });
PS: ? 現(xiàn)在附上PDO方式的連接測試代碼
$tns?=?"?? (DESCRIPTION?= ????(ADDRESS_LIST?= ??????(ADDRESS?=?(PROTOCOL?=?TCP)(HOST?=?192.168.11.111)(PORT?=?1521)) ????) ????(CONNECT_DATA?= ??????(Service_Name?=?orcl) ????)? ??)?"; $db_username?=?"C##test1";???#這里不能使用?sys角色的用戶 $db_password?=?"111111"; try?{ ????$conn?=?new?PDO("oci:dbname="?.?$tns,?$db_username,?$db_password); }?catch?(PDOException?$e)?{ ????echo($e->getMessage()); } $sql?=?"select?*?from?users_data"; $rs?=?$conn->query($sql); $m?=?$rs->fetchAll();
oci_connect連接的測試代碼
$conn?=?oci_connect('C##test1',?'111111',?'192.168.11.111/orcl'); if?(!$conn)?{ ????$e?=?oci_error(); ????print?htmlentities($e['message']); ????exit; }else?{ ????echo?"連接oracle成功!"; }
最后關(guān)于 laravel框架連接oracle的方式是使用oci_connect的方式進行連接
?public?function?createConnection($tns,?array?$config,?array?$options) ????{ ????????//?add?fallback?in?case?driver?is?not?set,?will?use?pdo?instead ????????if?(!?in_array($config['driver'],?['oci8',?'pdo-via-oci8',?'oracle']))?{ ????????????return?parent::createConnection($tns,?$config,?$options); ????????} ????????$config?????????????=?$this->setCharset($config); ????????$options['charset']?=?$config['charset']; ????????return?new?Oci8($tns,?$config['username'],?$config['password'],?$options); ????}
如果當(dāng)初并未設(shè)定oracle或者oci8 ?或者pdo-via-oci8的話,則使用PDO方式,否則使用oci8的oci_connect方式進行連接
OK: ? ?我的整個搭建過程到此為止,如有問題敬請留言!