最好的教程是官方文档!
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
(
'/'
);
});
效果: