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的工作原理。