Home php教程 php手册 Laravel 4 初级教程之安装及入门

Laravel 4 初级教程之安装及入门

Jun 06, 2016 pm 08:18 PM
laravel getting Started Install

本文不推荐完全不懂PHP与MVC编程的人学习。本文不是 “一步一步跟我做” 教程。本文需要你付出一定的心智去解决一些或大或小的隐藏任务,以达到真正理解 Laravel

0. 默认条件

本文默认你已经有配置完善的PHP+MySQL运行环境,懂得PHP网站运行的基础知识。跟随本教程走完一遍,你将会得到一个基础的包含登录的简单blog系统,并将学会如何使用一些强大的Laravel插件和composer包(Laravel插件也是composer包)。

软件版本:PHP 5.4+,MySQL 5.1+

1. 安装

许多人被拦在了学习Laravel的第一步,安装。并不是因为安装教程有多复杂,而是因为【众所周知的原因】。在此我推荐一个composer全量中国镜像:。推荐“修改 composer 的配置文件”方式配置。我在写此教程时用此镜像测试,安装失败,若你也出现这种情况,可以尝试另一个composer中国镜像:。

镜像配置完成后,切换到你想要放置该网站的目录下,运行命令:

复制代码 代码如下:


composer create-project laravel/laravel learnlaravel

然后,稍等片刻,当前目录下就会出现一个叫 learnlaravel 的文件夹,这时候如果你通过浏览器访问 learnlaravel/public/ 目录,基本都会显示 Error in exception handler.  ,这是因为 learnlaravel/app/storage 目录没有777权限,设置好权限即可看见页面如下图:

恭喜你~Laravel安装成功!

不想配置镜像的同学,可以使用 Laravel 界非常著名的超超搞得安装神器:https://github.com/overtrue/latest-laravel

2. 必要插件安装及配置

我们使用著名的Sentry插件来构建登录等权限验证系统。

打开 ./composer.json ,变更为:

复制代码 代码如下:


"require": {
 "laravel/framework": "4.2.*",
 "cartalyst/sentry": "2.1.4"
},

然后,在项目根目录下运行命令

复制代码 代码如下:


composer update

然后稍等一会儿,它会提示 cartalyst/sentry 2.1.4安装完成。

同理,我们将安装一个开发用的非常强大的插件,way/generators,这是它在composer库中的名字。在 composer.json中增加:

复制代码 代码如下:


"require-dev": {
    "way/generators": "~2.0"
},

放在“require”的下面。

运行 composer update,之后在 ./app/config/app.php 中增加配置:

复制代码 代码如下:


'Way\Generators\GeneratorsServiceProvider'

安装完成过,在命令行中运行 php artisan,就可以看到这个插件带来的许多新的功能。

有人会问,为什么用了国内镜像还是如此之慢?其实composer在update的时候最慢的地方并不是下载,而是下载之前的依赖关系解析,由于Laravel依赖的composer包非常之多,PHP脚本的执行速度又比较慢,所以每次update等个两三分钟很正常,习惯就好。

3. 数据库建立及迁移

数据库配置文件位于 ./app/config/database.php,我们需要把“connections”中的“mysql”项改成我们需要的配置。下面是我的配置:

复制代码 代码如下:


'mysql' => array(
 'driver'    => 'mysql',
 'host'      => 'localhost',
 'database'  => 'laravel',
 'username'  => 'root',
 'password'  => 'password',
 'charset'   => 'utf8',
 'collation' => 'utf8_unicode_ci',
 'prefix'    => 'l4_',
),

 prefix为表前缀,这个Laravel会帮我们自动维护,大胆写上不用担心。

这时候你需要去数据库建立此数据库,然后在命令行中输入:

复制代码 代码如下:


php artisan migrate --package=cartalyst/sentry

执行完成后,,你的数据库里就有了5张表,这是sentry自己建立的。sentry在Laravel4下的配置详情见 ,我大致说一下:

在 ./app/config/app.php 中 相应的位置 分别增加以下两行:

复制代码 代码如下:


'Cartalyst\Sentry\SentryServiceProvider',
'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry',

权限系统的数据库配置到此为止。

我们的简单blog系统将会有两种元素,Article和Page,下面我们将创建articles和pages数据表,命令行运行:

复制代码 代码如下:


php artisan migrate:make create_articles_table --create=articles
php artisan migrate:make create_pages_table --create=pages

这时候,去到 ./app/database/migrations,将会看到多出了两个文件,这就是数据库迁移文件,过一会我们将操作artisan将这两个文件描述的两张表变成数据库中真实的两张表,放心,一切都是自动的。

下面,在***_create_articles_table.php中修改:

复制代码 代码如下:


Schema::create('articles', function(Blueprint $table)
{
 $table->increments('id');
 $table->string('title');
 $table->string('slug')->nullable();
 $table->text('body')->nullable();
 $table->string('image')->nullable();
 $table->integer('user_id');
 $table->timestamps();
});

在***_create_pages_table.php中修改:

复制代码 代码如下:


Schema::create('pages', function(Blueprint $table)
{
 $table->increments('id');
 $table->string('title');
 $table->string('slug')->nullable();
 $table->text('body')->nullable();
 $table->integer('user_id');
 $table->timestamps();
});

下面,就是见证奇迹的时刻,在命令行中运行:

复制代码 代码如下:


php artisan migrate

这时候数据库中的articles表和pages表就建立完成了。

4. 模型 Models

接下来我们将接触Laravel最为强大的部分,Eloquent ORM,真正提高生产力的地方,借用库克的话说一句,鹅妹子英!

我们在命令行运行下列语句以创建两个model:

复制代码 代码如下:


php artisan generate:model article
php artisan generate:model page

这时候,在 ./app/models/ 下就出现了两个model文件。这两个类继承了Laravel提供的核心类 \Eloquent。

5. 数据库填充

分别运行下列命令:

复制代码 代码如下:


php artisan generate:seed page
php artisan generate:seed article

这时,在 ./app/database/seeds/ 下就出现了两个新的文件,这就是我们的数据库填充文件。Laravel提供自动数据库填充,十分方便。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Which is better, Django or Laravel? Which is better, Django or Laravel? Mar 28, 2025 am 10:41 AM

Both Django and Laravel are full-stack frameworks. Django is suitable for Python developers and complex business logic, while Laravel is suitable for PHP developers and elegant syntax. 1.Django is based on Python and follows the "battery-complete" philosophy, suitable for rapid development and high concurrency. 2.Laravel is based on PHP, emphasizing the developer experience, and is suitable for small to medium-sized projects.

See all articles