Laravel 5.7.17 發佈

2018.12.14 Laravel正式發佈了5.7.17的版本。主要是一些新的query builder方法以及偵測MariaDB連線失效的訊息還有改進Postgres增加foreign keys的改進。

新的query builder方法針對INSERT INTO SELECT增加了一個insertUsing的方法。然後呢在SQL HAVING二個值之間多了一個havingBetween。

對新的版本有興趣的朋友,可以參考Laravel上的changelog。

GitHub 5.7 changelog:

v5.7.17
Added
Added Database\Query\Builder::insertUsing method (#26732, 8216b46)
Added Database\Query\Builder::havingBetween method (#26758)
Added Packets out of order. Expected string to DetectsLostConnections trait (#26760)
Added NOT VALID option for skipping validation when adding postgres foreign keys (#26775)
Fixed
Fixed: Using store on an uploaded file when you push an empty file (#26809)
Fixed hiding for hidden commands (#26781)

PHP管制檔案下載的方式

在許多時候,我們會不希望網友知道網址空間直接下載檔案,或是透過帳號密碼的管控才能下載時,該如何用程式做過濾及管制呢?

header("Content-type:application");
header("Content-Disposition: attachment; filename=file_name");
//file_name是預設下載時的檔名,可使用變數。
readfile("file");
//file是實際存放在你硬碟中要被下載的檔案,可使用變數。
exit(0);

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
//限制下載速度,使用 while 迴圈加上 sleep。

while (!feof($handle)) {
echo fread($handle, 8192);
sleep(1);
}

$content = "";
$fp = fopen("http://file-to-download.com/a-file.zip", "rb");

if (!$fp)
die("Error opening file.");
while (!feof($fp))
$content .= fread($fp, 2048);
fclose($fp);

$fp=fopen("local-file-name.zip", "w");
fwrite($fp, $content);
fclose($fp);

$OrgFileName = mb_convert_encoding($OrgFileName, 'BIG-5','UTF-8');

$real_path = dirname(__FILE__).'/upload/'.$FileName;

header('Pragma: public');

header('Expires: 0');

header('Cache-Control: must-revalidate, post-check=0, pre-check=0');

header('Cache-Control: public', false);

header('Content-Description: File Transfer');

header('Accept-Ranges: bytes');

// application/force-download 為通用類型,亦可判斷檔案格式做改變

header('Content-Type: application/force-download');

header('Content-Length: '.filesize($real_path));

header('Content-Transfer-Encoding: binary');

// 注意「"」不可省略,否則FireFox會無法正確讀取

header('Content-Disposition:attachment;filename="'.$OrgFileName.'"');

// 讀取來源檔案資料

if ($stream = fopen($real_path, 'rb')){

while(!feof($stream) && connection_status() == 0){

echo(fread($stream,1024*8));

flush();

}

fclose($stream);

}

header('Content-type:application/force-download'); //告訴瀏覽器 為下載
header('Content-Transfer-Encoding: Binary'); //編碼方式
header('Content-Disposition:attachment;filename='.$filename); //檔名
@readfile($filename);

Windows Server 2016 安裝 PHP Laravel

一、開啟CGI功能
控制台→程式與功能→開啟或關閉Windows功能
開啟CGI

二、下載安裝PHP
http://windows.php.net/download/
三、修改php.ini
fastcgi.impersonate = 1
fastcgi.logging = 0
cgi.fix_pathinfo=1
cgi.force_redirect = 0
date.timezone = “Asia/Taipei”
extension_dir = “C:\PHP\ext”
四、安裝Visual C++ 可轉散發套件
五、IIS設定處理常式對應
六、IIS設定預設文件index.php
七、安裝URL Rewrite
https://www.iis.net/downloads/microsoft/url-rewrite

Laravel 5.4連線MS SQL Server方式

1. config/database.php 新增
[code]
// 2017.04.05 by lin shou shan
‘sqlsrv’ => [
‘driver’ => ‘sqlsrv’, //
‘host’ => ‘localhost\sqlexpress’, // 資料庫位置及別名
‘username’ => ‘sa’, //密號
‘password’ => ”, //密碼
‘database’ => ”, //資料庫
‘prefix’ => ”,
],
[/code]
2..env修改
DB_CONNECTION=sqlsrv

3.route,web.php測試
Route::get(‘/’, function () {
var_dump( DB::table(‘tablename’)->first() );
});

CodeIgniter on Win10記得裝ODBC Driver 11 for SQL Server

在新的Win10上裝上XAMPP後發現CI連不上SQL Server,於是先用原生的方式找到解決的方法

$serverName = "localhost\sqlexpress";
$connectionInfo = array( "Database"=>"Pos", "UID"=>"sa", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.
";
}else{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}

才知道WIN10預設沒有ODBC Driver 11 for SQL Server

https://www.microsoft.com/zh-TW/download/details.aspx?id=36434

Laravel 文章管理前後台實作(前台)

前台模板:

Start Bootstrap – Blog Home

Start Bootstrap – Blog Post

建立Laravel 專案

建立專案:命令提示字元執行

composer create-project laravel/laravel blog

新增資料庫:名稱blog(記得要改為 utf8_general_ci)

修改 app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema;  //這一行新增

public function boot()
{
   Schema::defaultStringLength(191); //這一行新增
}

修正時區 config/app.php

'timezone' => 'Asia/Taipei',

修改 .env環境檔

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=

建資料庫

php artisan migrate

建立認證機制

php artisan make:auth

建立含migration的model

php artisan make:model –migration Post

修改database\migrations\XXXXcreateposts_table.php

    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body')->nullable();
        $table->integer('user_id');
        $table->timestamps();
    });

建立posts資料表

php artisan migrate

建立預設資料

php artisan make:seeder PostSeeder

修改database\seeds\PostSeeder.php

public function run()
{
    DB::table('posts')->delete();

    for ($i=0; $i < 10; $i++) {
        \App\Post::create([
            'title'   => 'Title '.$i,
            'body'    => 'Body '.$i,
            'user_id' => 1,
        ]);
    }
}    

修改database\seeds\databaseSeeder.php

public function run()
{
    // $this->call(UsersTableSeeder::class);
    $this->call(PostSeeder::class);
}

填入資料

composer dump-autoload

php artisan db:seed

前台

app\Http\Controllers\HomeController.php

public function index()
{
    //return view('home');
    return view('home')->withPosts(\App\Post::all());
}

修改view

1 將Blog Home模板解壓縮

2 css、font、js目錄移至public資料夾

3 home.blade.php修改如下

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>部落格</title>

<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="css/blog-home.css" rel="stylesheet">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

<body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">部落格</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">

                <!-- Authentication Links -->
                @if (Auth::guest())
                    <li><a href="{{ url('/login') }}">登入</a></li>
                    <li><a href="{{ url('/register') }}">註冊</a></li>
                @else
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                            {{ Auth::user()->name }} <span class="caret"></span>
                        </a>

                        <ul class="dropdown-menu" role="menu">
                            <li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>登出</a></li>
                        </ul>
                    </li>
                @endif                    
            </ul>
        </div>
        <!-- /.navbar-collapse -->

    </div>
    <!-- /.container -->
</nav>

<!-- Page Content -->
<div class="container">

    <div class="row">

        <!-- Blog Entries Column -->
        <div class="col-md-12">

            <h1 class="page-header">
                部落格
                <small>文章列表</small>
            </h1>

            <!-- First Blog Post -->
            @foreach ($posts as $post)
            <h2>
                <a href="#">{{ $post->title }}</a>
            </h2>
            <p class="lead">
                作者: <a href="index.php">{{ $post->user_id }}</a>
            </p>
            <p><span class="glyphicon glyphicon-time"></span> 建立日期: {{ $post->created_at }}</p>

            <p>{{ $post->body }}</p>
            <a class="btn btn-primary" href="#">讀取更多 <span class="glyphicon glyphicon-chevron-right"></span></a>

            <hr>
            @endforeach    



        </div>



    </div>
    <!-- /.row -->

    <hr>

    <!-- Footer -->
    <footer>
        <div class="row">
            <div class="col-lg-12">
                <p>Copyright &copy; 林壽山 2017</p>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
    </footer>

</div>
<!-- /.container -->

<!-- jQuery -->
<script src="js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>

</body>

</html>

調整routes\web.php

Auth::routes();
Route::get('/', 'HomeController@index');
Route::get('/logout', 'Auth\LoginController@logout');
Route::get('/home', 'HomeController@index');

新增PostController

php artisan make:controller PostController

新增函數

public function show($id)
{
    return view('post/show')->withPosts(\App\Post::find($id));
}

新增路由

Route::get('post/{id}', 'PostController@show'); 

建立view

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>部落格</title>

<!-- Bootstrap Core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="/css/blog-home.css" rel="stylesheet">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

<body>

<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="{{ url('/') }}">部落格</a>
        </div>
        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">

                <!-- Authentication Links -->
                @if (Auth::guest())
                    <li><a href="{{ url('/login') }}">登入</a></li>
                    <li><a href="{{ url('/register') }}">註冊</a></li>
                @else
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                            {{ Auth::user()->name }} <span class="caret"></span>
                        </a>

                        <ul class="dropdown-menu" role="menu">
                            <li><a href="{{ url('/logout') }}"><i class="fa fa-btn fa-sign-out"></i>登出</a></li>
                        </ul>
                    </li>
                @endif                    
            </ul>
        </div>
        <!-- /.navbar-collapse -->

    </div>
    <!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">

    <div class="row">

        <!-- Blog Post Content Column -->
        <div class="col-lg-8">

            <!-- Blog Post -->

            <!-- Title -->
            <h1>{{ $posts->title }}</h1>

            <!-- Author -->
            <p class="lead">
                作者: <a href="#"> {{ $posts->user_id }}</a>
            </p>

            <hr>

            <!-- Date/Time -->
            <p><span class="glyphicon glyphicon-time"></span> 日期:  {{ $posts->updated_at }}</p>


            <!-- Post Content -->
            <p> {{ $posts->body }}</p>





        </div>




    </div>
    <!-- /.row -->

    <hr>

    <!-- Footer -->
    <footer>
        <div class="row">
            <div class="col-lg-12">
                <p>Copyright &copy; 林壽山 2017</p>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
    </footer>


</div>    
<!-- /.container -->

<!-- jQuery -->
<script src="/js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>

</body>

</html>