laravel 安裝 windows下nginx環境

安裝的時候本想安最新的laravel8 結果發現本地php版本是7.0.13,最終只能安裝laravel5.5

 1,查看php版本

PS D:\Visual-NMP-x64\www\Demo1\feng_app> php -v
PHP 7.0.13 (cli) (built: Nov  8 2016 13:33:09) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.13, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans

2,查看composer 版本

PS D:\Visual-NMP-x64\www\Demo1\feng_app> composer -V
Composer version 2.1.9 2021-10-05 09:47:38

3,執行安裝

PS D:\Visual-NMP-x64\www\Demo1> composer create-project laravel/laravel feng_app
Creating a "laravel/laravel" project at "./feng_app"
Installing laravel/laravel (v5.5.28)
  - Downloading laravel/laravel (v5.5.28)
  - Installing laravel/laravel (v5.5.28): Extracting archive
Created project in D:\Visual-NMP-x64\www\Demo1\feng_app

4,因爲使用的是nginx 需要配置nginx的環境

server {
        #sitename    Demo1
        listen       20081;
        server_name  localhost;
        root         D:/Visual-NMP-x64/www/Demo1/feng_app/public; 
        #error_log    D:/Visual-NMP-x64/logs/Nginx/D__Visual-NMP-x64_www_Demo1-error.log;
        #access_log   D:/Visual-NMP-x64/logs/Nginx/D__Visual-NMP-x64_www_Demo1-access.log;       
        autoindex    on;
        index        index.php index.html index.htm;
        
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

 
        location / {
        try_files $uri $uri/ /index.php?$query_string;
        
        }                
        location  ~ [^/]\.php(/|$) {
                fastcgi_split_path_info  ^(.+?\.php)(/.*)$;
                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }
                fastcgi_pass   127.0.0.1:9002;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO        $fastcgi_path_info;
                fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
                include        fastcgi_params;
        }
    }

 

5,查看效果如圖

 

6,創建控制器

PS D:\Visual-NMP-x64\www\Demo1\feng_app> php artisan make:controller  IndexController
Controller created successfully.
PS D:\Visual-NMP-x64\www\Demo1\feng_app> php artisan make:controller  Admin/IndexController
Controller created successfully.

控制器代碼

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
     public function index()
    {
        return view('index');
    }
}

顯示效果

 

路由web.php代碼

Route::get('/index',  'IndexController@index');

Route::get('/', function () {
    return view('welcome');
});

Route::get('/test', function () {
    return 'feng_app is working';
});

訪問test的顯示效果

7,配置數據庫mysql和redis

env文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feng_app
DB_USERNAME=root
DB_PASSWORD=123456

 

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

安裝redis包

PS D:\Visual-NMP-x64\www\Demo1\feng_app> composer require predis/predis

 

修改IndexController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class IndexController extends Controller
{
     public function index(Request $request)
    {
        $n = $request->input('name','nnn');
        $t = Redis::get('test');
        Redis::set('name', $n);
        Redis::setex('str', 10, 'bar');
        $c=Redis::exists('foo');
        if($c){Redis::del('name');}
        $users = DB::select('select * from admin_users where id = ?', [1]);
        return view('index',['users' => $users,'redis_test'=>$t]);
    }
}

 

修改index.blade.php模板

 <div class="content">
                <div class="title m-b-md">
                    index
                </div>
                redis:{{$redis_test}}<br>
                mysql:
                @foreach ($users as $r)
                    {{ $r->id }}  {{ $r->name }} {{ $r->username }} <br> 
                @endforeach
                
            </div>

顯示效果

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章