Skip to content

Instantly share code, notes, and snippets.

@rambolee
Last active October 25, 2016 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rambolee/c391eb9d24c4d5b4b579c22b48acdd39 to your computer and use it in GitHub Desktop.
Save rambolee/c391eb9d24c4d5b4b579c22b48acdd39 to your computer and use it in GitHub Desktop.

laravel quick start (Part I)

对应 bash 的 laravel 手册的 Guides-> Basic Task List

学习 laravel,开始根据中文文档创建了一个 laravel new blog 模板。 但是,发现入手很困难。之前团队内部的分享也忘记的差不多了。发现技术有些脱节,需要跟上新的 php 的版本和框架的节奏。

顾,从官方文档入手。 https://laravel.com/docs/5.2/quickstart 开始重新再学习update 一下。

installing the Quickstart

git clone https://github.com/laravel/quickstart-basic quickstart
cd quickstart
composer install
php artisan migrate

设置目录权限,否则无法访问

chmod 777 -R storage bootstrap/cache

默认 git 下来的项目是没有 vendor 目录的,因此,需要安装更新 如果这里不进行安装,就会发现页面报出无法找到 vendor/autoload.php相关的错误,和 openbase_dir 相关错误。这里完全是个经验问题。

composer install

然后刷新页面见到: !install_error

修改数据库配置 config_db

创建数据表

Database Migrations

php artisan make:migration create_tasks_table --create=tasks
Created Migration: 2016_03_29_055204_create_tasks_table

The migration will be placed in the database/migrations directory of your project. As you may have noticed, the make:migration command already added an auto-incrementing ID and timestamps to the migration file.

Let's edit this file and add an additional string column for the name of our tasks:

pic_001

To run our migration, we will use the migrate Artisan command. If you are using Homestead, you should run this command from within your virtual machine, since your host machine will not have direct access to the database:

php artisan migrate

pic_002  This command will create all of our database tables. If you inspect the database tables using the database client of your choice, you should see a new tasks table which contains the columns defined in our migration. Next, we're ready to define an Eloquent ORM model for our tasks!

pic_003

Eloquent Models

Eloquent is Laravel’s default ORM(object-relational mapper). 使用artisan 工具进行创建。可以指定具体生成路径。例如,根据之前使用 Yii 的使用习惯,比较喜欢将 Model 放到统一的目录中,如果不去指定路径,如下面命令,则会讲 model 直接保存到 app\ 目录下。

php artisan make:model Task

model_task  因此,我个人比较喜欢使用如下方式:

php artisan make:model Models\Task

效果如下:  model_task

Routing

Stubbing The Routes 理解了一下 Demo 里面的 routes 的代码 app/Http/routes.php 的代码和官方文档

Routes are used to point URLs to controllers or anonymous functions that should be executed when a user accesses a given page. By default, all Laravel routes are defined in the app/Http/routes.php file that is included in every new project.

For this application, we know 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. We'll wrap all of these routes in the web middleware so they have session state and CSRF protection. So, let's stub all of these routes in the app/Http/routes.php file.

从这里可以分析出,Routes 的作用有几点:

  • URLs 访问的入口
  • 匿名函数(anonymous functions)的访问入口
  • 默认情况下都在 app/Http/routes.php下面

作为 API整体入口,读取到获取方式。首先是 ‘/‘ 针对跟目录的访问。

Route::get('/', function () {
    return view('tasks');
});

这里有两点要说明:

  • view 这个 helper 的出现,Laravel 默认将 HTML 的模板都保存在 resources/views 下面。view helper 会返回对应的 template 的内容。
  • 对应的 template 的文件命名需要是 tasks.blade.php(在 resources/views/)目录下

这里又衍生出 Blade Template Engine 的概念 template_engine

上面是一段 template 的代码。其中值得注意的是 yield 关键字

这里读到一篇关于 yield 的解释,涉及的关键词:PHP Generator syntax

我目前的理解,使用 yield 的缘由是手册这里提及的缘由:

Note the @yield('content') portion of the layout. This is a special Blade directive 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 primary content. 也就是说,content 的部分主要本分,但是是「next」 因此,是最后渲染的部分。而且,yield 是协程的概念,非抢占式。应该是因为上述原因使用。—这里具体还需要深究一下。「Mark」

Defining The Child View

这里编辑对应 task 的 View 的代码模块,如下: template_engine

其中@extends(‘layouts.app’) 对应的就是 resources/views/layouts/app.blade.php 这个路径下的文件。 在@section(‘content’)和@endsection 会「injected」到@yield(‘content’) 中,也就是 app.blade.php 的 layout 中。

@include(‘common.errors’) 会加载 resources/views/common/errors.blade.php,这里官网文档提到

We haven't defined this template, but we will soon! 没有 get 到他们的意思。

因此,根据上面 app.blade.php 和 tasks.blade.php 和其他几个通用文件的加载,完成了一个基础的页面的加载(a basic layout and view for our application),也就是 如下代码的最终加载过程。

Route::get('/', function () {
    return view('tasks');
});

下面开始增加向页面 Form 中POST 数据的处理代码。

Adding Tasks

Validation

  • 配置name字段,要求长度不能大于255个字符。
  • 如果验证失败,跳转到 ‘/‘ URL 下,并提示(flash)the old input and errors into session。
Route::post('/task', function (Request $request) {
    $validator = Validator::make($request->all(), [
        'name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        return redirect('/')
            ->withInput()
            ->withErrors($validator);
    }

    // Create The Task...
});

Creating The Task

就在上面 Validation 的基础上,进行: validation  主要使用 ORM 中的 save 方法。 这样就完成了创建 tasks。下面开始编写 list all tasks 的相关 code

###Displaying Existing Tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment