Laravel是一個免費的開源PHP Web框架,由泰勒·奧特威爾(Taylor Otwell)創建,旨在遵循模型 - 視圖– Controller(MVC)體系結構模式,並基於Symfony,用於開發Web應用程序。 Laravel的某些功能是一個模塊化包裝系統,具有專用依賴關係管理器,訪問關係數據庫的不同方法,有助於應用程序部署和維護的實用程序以及其對句法糖的方向。
模型組件對應於用戶使用的所有與數據相關的邏輯。這可以表示視圖和控制器組件之間要傳輸的數據或任何其他與業務邏輯相關的數據。
文件夾:app/
視圖組件用於應用程序的所有UI邏輯。
文件夾:資源/視圖
控制器充當模型和視圖組件之間的接口,以處理所有業務邏輯和傳入請求,使用模型組件來操縱數據並與視圖進行交互以呈現最終輸出。
文件夾:App/HTTP/Controller
該項目的要求:
"php": "^7.1.3",
"laravel/framework": "5.7.*",
Laravel設置:
Install PHP 7.1.3
Instal MySQL
Install Composer
更新供應商文件夾:
composer update
or
composer install
在CMD上輸入以創建項目:
composer global require "laravel/installer"
laravel new blogname
檢查Laravel中的可用命令或參考
php artisan list
php artisan help [command name]
ex. php artisan help make:model
運行Laravel
php artisan serve
or
in browser http://localhost/laravel/magelaravel/public/
.env中的設置數據庫
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homested
DB_USERNAME=homestead
DB_PASSWORD=secret
設置身份驗證(可選):
php artisan make:auth
設置數據庫配置
php artisan migrate
php artisan migrate:refresh //if you want to change laravel model
設置電子郵件用於測試目的
MAIL_DRIVER=log
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
or
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mageCE0721
MAIL_ENCRYPTION=tls
設置Homecontroller來管理網站路線。 app/http/controllers/homecontroller.php
public function index()
{
$post = Post::all();
return view( 'index' , ['post' => $post ]);
}
public function detail(Request $request)
{
$post = Post::find($request->post);
return view('detail' , ['post' => $post ]);
}
public function home()
{
$post = Post::all();
return view( 'home' , ['post' => $post ]);
}
public function edit(Request $request)
{
$post = Post::find($request->idpost);
return view( 'edit' , ['post' => $post ]);
}
創建模型,控制器,遷移的遷移
php artisan make:controller PostController
php artisan make:model Post
php artisan make:migration create_post_table --create=posts
or
php artisan make:model Post -mc
製作crud(創建讀取更新刪除),然後將所有內容放入postcontroller.php中以控制數據管理應用程序/http/controllers/postcontroller.php
public function create(Request $request)
{
// Validate the request...
$request->validate([
'title' => 'required',
'content' => 'required',
]);
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->username = auth()->user()->name;//auth()->id()
$post->save();
return back();
}
public function update(Request $request)
{
// Validate the request...
$request->validate([
'title' => 'required',
'content' => 'required',
]);
$post = Post::find($request->idpost);
$post->title = $request->title;
$post->content = $request->content;
$post->save();
return redirect('/home');
}
public function delete(Request $request)
{
$post = Post::find($request->idpost);
$post->delete();
return back();
}
要使用控制器將其放在路由/web.php中,您可以將代碼直接放入路由/web.php中
Route::get('/', 'HomeController@index');
Route::get('/detail/{post}', 'HomeController@detail');
Auth::routes();
Route::get('/home', 'HomeController@home')->name('home');
Route::get('/home/edit/{idpost}', 'HomeController@edit');
Route::post('/post/create', 'PostController@create');
Route::post('/post/update/{idpost}', 'PostController@update');
Route::get('/post/delete/{idpost}', 'PostController@delete');
使用刀片模板管理網站的視圖。使用刀片模板使創建HTML頁面更加容易。例子:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8">
@foreach ($post as $p)
<p>
<h4><a href="/detail/{{$p->id}}">{{ $p->title }}</a></h4>
{{ $p->created_at->toFormattedDateString() }} <span style="float:right">Author : {{ $p->username }}</span>
</p>
@endforeach
</div>
</div>
</div>
@endsection
在Laravel中,有兩種控制數據庫的方法。與查詢建造者和雄辯。雄辯是由Laravel創建的,以使管理數據庫更容易
Get Data Example with Query Bulider
DB::table('users')->get();
DB::table('users')->where('name', 'John')->first();
Get Data Example with Eloquent
AppFlight::all();
AppFlight::where('active', 1)->orderBy('name', 'desc')->take(10)->get();
一些基本的使用Laravel。您可以使用此項目進行參考。在您自己的計算機上運行此項目,以了解基本的Laravel的工作原理。