Laravel5.5前後臺分離
阿新 • • 發佈:2019-01-07
經過網上查資料及自己摸索,終於實現了自己所想要實現的前後臺分離
PS:在模板這一塊感覺還不夠完美,如果後邊找到更好的方法,到時會到這裡更新。
前臺域名:www.test6.local後臺域名:admin.test6.local環境為:Win7x64+PHPStudy2018 nginx+PHP7.0
nginx.conf增加域名解析建立兩個資料夾/public/www/public/admin將index.php拷到上述資料夾中,注意修改require路徑加../前後分離的關鍵點在/app/Providers/RouteServiceProvider.php增加或修改如下程式碼:#test6 www server { listen 80; server_name www.test6.local; root "d:/data/www/test6/public/www"; index index.html index.htm index.php; error_page 404 /404.html; location = /404.html { return 404 'Sorry, File not Found!'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; # windows dir } location / { try_files $uri $uri/ /index.php?$query_string; } #location / { # try_files $uri @rewrite; #} #location @rewrite { # set $static 0; # if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) { # set $static 1; # } # if ($static = 0) { # rewrite ^/(.*)$ /index.php?s=/$1; # } #} location ~ /Uploads/.*\.php$ { deny all; } location ~ \.php/ { if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_NAME $1; fastcgi_param PATH_INFO $2; fastcgi_param SCRIPT_FILENAME $document_root$1; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
/routes下新增兩個檔案www.php admin.php路由自己發揮,如://前臺名稱空間 protected $www_namespace = 'App\Http\Controllers\Www'; //後臺名稱空間 protected $admin_namespace = 'App\Http\Controllers\Admin'; //$this->mapApiRoutes(); //$this->mapWebRoutes(); //判斷域名字首 如admin,www $url_prefix = explode('.',$_SERVER['HTTP_HOST'])[0]; if($url_prefix=='www'){ $this->mapWwwRoutes(); }else if($url_prefix=='admin'){ $this->mapAdminRoutes(); } //前臺路由 protected function mapWwwRoutes(){ //echo 2333;exit; Route::middleware('web') ->namespace($this->www_namespace) ->group(base_path('routes/www.php')); } //後臺路由 protected function mapAdminRoutes(){ //echo 2333;exit; Route::middleware('web') ->namespace($this->admin_namespace) ->group(base_path('routes/admin.php')); }
Route::get('/', '[email protected]');
/app/Htpp/Controllers下新增兩個資料夾,為Www和Admin檔案如:【Admin下Controller.php修改】namespace App\Http\Controllers\Admin;
【Admin下IndexController.php修改】namespace App\Http\Controllers\Admin;
前後臺模板頁分離測試在resources/views下新建Www和Admin資料夾,將模板檔案放入在前述控制器中這樣寫:這樣就達到了前後臺分離的目的。return view('Www/index',compact('title','list','info','email'));