Laravel 学习 第一天

今天主要的学习任务有以下

安装

安装Composer

Compser是PHP依赖管理工具,

curl -sS https://getcomposer.org/installer | php

如果提示HTTPS证书问题可以改用如下命令行安装以忽略证书

curl -k https://getcomposer.org/installer | php

安装完Composer后,使用Composer来安装laravel

php composer.phar global require "laravel/installer=~1.1"

安装完成后将laravel加到PATH路径中,在MAC上修改PATH变量

添加完了后就可以使用一下命令在当前目录下创建项目了。

laravel new Project_Name

laravel 要求PHP版本大于等于5.4

在Laravel 5中调整public文件夹的名称为public_html

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

all set, let's start building

Let's start coding

配置

所有和应用相关的配置都保存在config文件夹中

读取Config变量

$value = Config::get('app.timezone'); // 可以通过外观模式来读写Config里定义的变量
Config::set('app.timezone', 'America/Chicago');
$value = config('app.timezone'); // 也可以通过帮助函数来读取Config的值

storagevendor文件夹需要写权限

修改namespace的名称可以使用如下命令行

php artisan app:name Car

维护模式 - 这个可以有

php artisan down
php artisan up

维护期间的提示页模板 resources/views/errors/503.blade.php

在项目根目录下有一个.env文件,在这里定义的所有常量都可以通过$_ENV 访问。工程师各自的开发环境会有不同,所以这个文件最好不要提交到SourceControl中。

获取并检测当前应用的环境

$environment = app()->environment(); // helper function 
$environment = App::environment(); // app facade

if ($app->environment('local')) {// The environment is local}
if ($app->environment('local', 'staging')){ // The environment is either local OR staging...}

MARK 文档里有下面一句没有看懂什么意思,估计等到学Service Container的时候会搞明白

To obtain an instance of the application, resolve the Illuminate\Contracts\Foundation\Application
contract via the service container.

The Basics

Routing

Laravel的路由做的还是很优雅的,用这套路由机制做RESTful API实在是太爽了(相比CI)

路由分组

这是个很方便的工具,可以对多个路由分组,将

Middleware

这货让我想起了很久之前.net framework里的http handler。。。
所有的middleware都存在App/Http/Middleware/这个文件夹中

创建Middleware

执行下面的语句来创建新的Middleware

php artisan make:middleware ImageFetcher

执行成功会在middleware的文件夹中多一个ImageFetcher.php的文件。

<?php namespace App\Http\Middleware;

use Closure;

class ImageFetcher {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
            // Perform action before request is handled by app
            $response = $next($request);
            // Perform action after request is handled by app
        return $response;
    }
}

注册

Middleware做完了之后需要注册到应用中去,app/Http/这个文件夹下有Kernal.php文件,全局的middleware注册在$middleware变量中;如果是要给某个特定的Route绑定使用的middleware,注册在$routeMiddleware这个变量中。

 Route::get('admin/profile', ['middleware' => 'auth', function(){
    // do the shit here
}]);

可结束的Middleware

有的操作需要在http请求被处理之后才可以做,例如Session要在http请求结束后才会往客户端写数据。碰到这种情况,将middleware声明为TerminableMiddleware,在termintate这个函数中进行http请求完成后的操作。

use Closure;
use Illuminate\Contracts\Routing\TerminableMiddleware;

class StartSession implements TerminableMiddleware {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Store the session data...
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接 必备品 文档:Documentation API:API Reference 视频:Laracasts ...
    layjoy阅读 8,643评论 0 121
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • 0.1配置1.模板继承2.控制器3.git4.支付宝支付的流程5.路由6.中间件7.请求8.laravel 学习笔...
    云龙789阅读 842评论 0 5
  • 先说几句废话,调和气氛。事情的起由来自客户需求频繁变更,伟大的师傅决定横刀立马的改革使用新的框架(created ...
    wsdadan阅读 3,115评论 0 12
  • 迎着朝阳 追着月光 想这样远走他乡 捧着书卷 写着笔记 想这样必成大器 望着灯光 叹着热气 想这样长眠不起 记着亲...
    泊杨阅读 207评论 0 1