最好的教程是官方文檔!
homestead安裝好,就可使用了。
安裝Laravel
composer create-project --prefer-dist laravel/laravel blog
若是首頁打不開,
chmod 777 -R storage/
chmod 777 -R bootstrap/cache/
laravel
new
command will create a fresh Laravel installation in the directory you specify. For instance,
laravel new blog
will create a directory named
blog
containing a fresh Laravel installation with all of Laravel's dependencies already installed.
其中
app包含了站點的 controllers(控制器),models(模型),views(視圖)和 assets(資源)。這些是網站運行的主要代碼,你會將你大部分的時間花在這些上面。
bootstrap用來存放系統啓動時須要的文件,這些文件會被如 index.php 這樣的文件調用。
public
文件夾是惟一外界能夠看到的,是必須指向你 web 服務器的目錄。它含有 laravel 框架核心的引導文件 index.php
vendor用來存放全部的第三方代碼
Laravel(下面簡稱L)內置了很是完善好用的簡單用戶登陸註冊功能,激活這個功能很是容易,運行如下命令:
php
artisan
make
:
auth
頁面不正常,好像是由於
5.1 版本去掉了本系列教程主要講解的元素(Auth 系統)
composer create-project laravel/laravel learnlaravel5 5.0.22 //
第三個參數中指定版本號
查看app/Http/routes.php 的代碼
視圖在resources\views下
必定要把網站定位到public目錄下,否則訪問/home會失敗。可經過修改Homestead.yaml,而後
vagrant provision
重啓。
==========================================
數據庫:
php artisan make
:
migration create_tasks_table
--
create
=
tasks
database
/
migrations
directory of your project.
能夠把phpmyadmin放在public 目錄下,而後修改腳本權限: chmod a-w *.php
Let's edit this file (如2016_08_05_051404_create_tasks_table.php)and add an additional
string
column
$table->string('name');//add
php artisan migrate
This command will create all of our database tables.
Eloquent Models
Usually, each Eloquent model corresponds directly with a single database table.
let's define a
Task
model that corresponds to our
tasks
database table we just created.
php artisan make
:
model Task
The model will be placed in the
app
directory
model name會假設對錶的表是model name的複數形式,如Task model對應tasks表
defined in the
app
/
Http
/
routes
.
php
在此處,we will need at least three routes: a route to display a list of all of our tasks, a route to add new tasks, and a route to delete existing tasks.
Displaying A View
HTML templates are stored in the
resources
/
views
directory
像每一個頁面都會用到的導航,能夠經過Blade
layouts
實現
let's define a new
layout
view in
resources
/
views
/
layouts
/
app
.
blade
.
php
. The
.
blade
.
php
extension instructs the framework to use the
Blade templating engine
to render the view.
@yield('content') 是一個Blade
指令:
that specifies where all child pages that extend the layout can inject their own content.
Next, let's define the
child
view that will
use this layout
and provide its
content.
Defining The Child View
resources
/
views
/
tasks
.
blade
.
php
All of the content between
@
section
(
'content'
)
and
@endsection
will be
injected
into the location of the
@
yield
(
'content'
)
The
@
include
(
'common.errors'
)
directive will load the
template
located at
resources
/
views
/
common
/
errors
.
blade
.
php
.
<!-- resources/views/common/errors.blade.php -->
@
if
(
count
(
$errors
)
>
0
)
<!-- Form Error List -->
<
div class
="
alert alert-danger
">
<
strong
>
Whoops
!
Something went wrong
!
</
strong
>
<
br
><
br
>
<
ul
>
@
foreach
(
$errors
->all
()
as
$error
)
<
li
>{{
$error
}}</
li
>
@
endforeach
</
ul
>
</
div
>
@
endif
測試錯誤輸出:
將輸入數據取不合要求的格式便可。
這個錯誤格式只有include它的view才能顯示:
@
include
(
'common.errors'
)
檢驗輸入數據:如少於255個字符
若是校驗失敗,將重定向用戶到/目錄
->withErrors
(
$validator
)
will
flash
the errors from the given validator instance
into the session
so that they can be accessed via the
$errors
variable in our view.
Creating The Task
To create the task, we may use the
save
method after creating and setting properties on a new Eloquent model:
$task = new Task;
$task->name = $request->name;
$task->save();
如今的樣子:
Displaying Existing Tasks
we need to edit our
/
route to
pass
all of the existing tasks
to the view
.
The
view
function accepts a
second
argument which is an
array
of data that will be made available to the view, where each
key
in the array will become a
變量
within the view:
Adding The Delete Button
When the button is clicked, a
DELETE
/
task
request will be sent to the application:
Note that the delete button's form
method
is listed as
POST
, even though we are responding to the request using a
Route
::
delete
route.
HTML forms only allow the
GET
and
POST
HTTP verbs, so we need a way to spoof a
DELETE
request from the form.
We can spoof a
DELETE
request by outputting the results of the
method_field
(
'DELETE'
)
function within our form. This function generates a hidden form input that Laravel recognizes and will use to
override
the actual HTTP request method. The generated field will look like the following:
<
input type
="
hidden
"
name
="
_method
"
value
="
DELETE
">
We can use
implicit model binding
to automatically retrieve the
Task
model that corresponds to the
{task}
route parameter
.
Route
::
delete
(
'/task/
{task}
'
,
function
(
Task
$task
)
{
$task
->delete
();
return
redirect
(
'/'
);
});
效果: