国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Laravel 5 框架入門(一),laravel框架入門
Home php教程 php手冊(cè) Laravel 5 框架入門(一),laravel框架入門

Laravel 5 框架入門(一),laravel框架入門

Jun 13, 2016 am 09:07 AM
laravel Getting Started Tutorial

Laravel 5 框架入門(一),laravel框架入門

Laravel 5 中文文檔:

1. http://laravel-china.org/docs/5.0

2. http://www.golaravel.com/laravel/docs/5.0/

默認(rèn)條件

本文默認(rèn)你已經(jīng)有配置完善的 PHP + MySQL 運(yùn)行環(huán)境,懂得 PHP 網(wǎng)站運(yùn)行的基礎(chǔ)知識(shí)。跟隨本教程走完一遍,你將會(huì)得到一個(gè)基礎(chǔ)的包含登錄的簡(jiǎn)單 blog 系統(tǒng),并將學(xué)會(huì)如何使用一些強(qiáng)大的 Laravel 插件和 composer 包(Laravel 插件也是 composer 包)。

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

本文不推薦完全不懂 PHP 與 MVC 編程的人學(xué)習(xí)。本文不是 “一步一步跟我做” 教程。本文需要你付出一定的心智去解決一些或大或小的隱藏任務(wù),以達(dá)到真正理解 Laravel 運(yùn)行邏輯的目的。

1. 安裝

許多人被攔在了學(xué)習(xí)Laravel的第一步,安裝。并不是因?yàn)榘惭b教程有多復(fù)雜,而是因?yàn)椤颈娝苤脑颉?。在此我推薦一個(gè)composer全量中國(guó)鏡像:http://pkg.phpcomposer.com/ 。推薦以 “修改 composer 的配置文件” 方式配置。

鏡像配置完成后,切換到你想要放置該網(wǎng)站的目錄下(如 C:\\wwwroot、/Library/WebServer/Documents/、/var/www/html、/etc/nginx/html 等),運(yùn)行命令:

composer create-project laravel/laravel learnlaravel5

然后,稍等片刻,當(dāng)前目錄下就會(huì)出現(xiàn)一個(gè)叫 learnlaravel5 的文件夾。

然后將網(wǎng)站根目錄配置為 learnlaravel5/public。

如果你不會(huì)配置,建議去學(xué)會(huì)配置,網(wǎng)上資料很多。如果自暴自棄,可以把 的第 29 行'url' => 'http://localhost', 配置成你的子目錄地址,注意,要一直配置到 ***/learnlaravel5/public。

使用瀏覽器訪問你配置的地址,將看到以下畫面(我在本地配置的地址為 http://fuck.io:88 ):


2. 體驗(yàn) Auth 系統(tǒng)并完成安裝

—— 經(jīng)過上面的過程,Laravel 5 的安裝成功了?

—— 沒有o(╯□╰)o

查看路由文件 `learnlaravel5/app/Http/routes.php` 的代碼:

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::controllers([
	'auth' => 'Auth\AuthController',
	'password' => 'Auth\PasswordController',
]);

跟隨代碼里的蛛絲馬跡,讓我們?cè)L問 http://fuck.io:88/home (請(qǐng)自行替換域名),結(jié)果竟然跳轉(zhuǎn)到了登陸頁?


沒錯(cuò),Laravel 自帶了開箱即用的 Auth 系統(tǒng),連頁面都已經(jīng)寫好了。

讓我們隨意輸入郵箱和密碼,點(diǎn)擊登錄,你很可能得到以下畫面(Mac 或 Linux 下):


為什么空白?用開發(fā)者工具查看,這個(gè)請(qǐng)求的狀態(tài)碼是 500,為什么?

因?yàn)?`learnlaravel5/storage` 目錄沒有 777 權(quán)限。

執(zhí)行 shell 命令:

cd learnlaravel5

sudo chmod -R 777 storage

重新訪問 http://fuck.io:88/home ,隨意輸入郵箱和密碼,如果你得到以下畫面:


那么恭喜你~ Laravel 5 安裝成功!

不想配置鏡像的同學(xué),可以使用 Laravel 界非常著名的 安正超 搞的安裝神器:https://github.com/overtrue/latest-laravel

3. 數(shù)據(jù)庫(kù)建立及遷移

Laravel 5 把數(shù)據(jù)庫(kù)配置的地方改到了 `learnlaravel5/.env`,打開這個(gè)文件,編輯下面四項(xiàng),修改為正確的信息:

DB_HOST=localhost

DB_DATABASE=laravel5

DB_USERNAME=root

DB_PASSWORD=password

推薦新建一個(gè)名為 laravel5 的數(shù)據(jù)庫(kù),為了學(xué)習(xí)方便,推薦使用 root 賬戶直接操作。

Laravel 已經(jīng)為我們準(zhǔn)備好了 Auth 部分的 migration,運(yùn)行以下命令執(zhí)行數(shù)據(jù)庫(kù)遷移操作:

php artisan migrate

得到的結(jié)果如下:


如果你運(yùn)行命令報(bào)錯(cuò),請(qǐng)檢查數(shù)據(jù)庫(kù)連接設(shè)置。

至此,數(shù)據(jù)庫(kù)遷移已完成,你可以打開 http://fuck.io:88/home 歡快地嘗試注冊(cè)、登錄啦。

4. 模型 Models

接下來我們將接觸Laravel最為強(qiáng)大的部分,Eloquent ORM,真正提高生產(chǎn)力的地方,借用庫(kù)克的一句話:鵝妹子英!

運(yùn)行一下命令:

php artisan make:model Article

php artisan make:model Page

> Laravel 4 時(shí)代,我們使用 Generator 插件來新建 Model?,F(xiàn)在,Laravel 5 已經(jīng)把 Generator 集成進(jìn)了 Artisan。

現(xiàn)在,Artisan 幫我們?cè)?`learnlaravel5/app/` 下創(chuàng)建了兩個(gè)文件 `Article.php` 和 `Page.php`,這是兩個(gè) Model 類,他們都繼承了 Laravel Eloquent 提供的 Model 類 `Illuminate\Database\Eloquent\Model`,且都在 `\App` 命名空間下。這里需要強(qiáng)調(diào)一下,用命令行的方式創(chuàng)建文件,和自己手動(dòng)創(chuàng)建文件沒有任何區(qū)別,你也可以嘗試自己創(chuàng)建這兩個(gè) Model 類。

Model 即為 MVC 中的 M,翻譯為 模型,負(fù)責(zé)跟數(shù)據(jù)庫(kù)交互。在 Eloquent 中,數(shù)據(jù)庫(kù)中每一張表對(duì)應(yīng)著一個(gè) Model 類(當(dāng)然也可以對(duì)應(yīng)多個(gè))。

如果你從其他框架轉(zhuǎn)過來,可能對(duì)這里一筆帶過的 Model 部分很不適應(yīng),沒辦法,是因?yàn)?Eloquent 實(shí)在太強(qiáng)大了啦,真的沒什么好做的,繼承一下 Eloquent 類就能實(shí)現(xiàn)很多很多功能了。

如果你想深入地了解 Eloquent,可以閱讀系列文章:Laravel 5框架學(xué)習(xí)之Eloquent 關(guān)系

接下來進(jìn)行 Article 和 Page 類對(duì)應(yīng)的 articles 表和 pages表的數(shù)據(jù)庫(kù)遷移,進(jìn)入 `learnlaravel5/database/migrations` 文件夾。

在 ***_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();
});

然后執(zhí)行命令:

php artisan migrate

成功以后, tables 表和 pages 表已經(jīng)出現(xiàn)在了數(shù)據(jù)庫(kù)里,去看看吧~

5. 數(shù)據(jù)庫(kù)填充 Seeder

在 `learnlaravel5/database/seeds/` 下新建 `PageTableSeeder.php` 文件,內(nèi)容如下:

<&#63;php

use Illuminate\Database\Seeder;
use App\Page;

class PageTableSeeder extends Seeder {

 public function run()
 {
  DB::table('pages')->delete();

  for ($i=0; $i < 10; $i++) {
   Page::create([
    'title'  => 'Title '.$i,
    'slug'  => 'first-page',
    'body'  => 'Body '.$i,
    'user_id' => 1,
   ]);
  }
 }

}

然后修改同一級(jí)目錄下的 `DatabaseSeeder.php`中:

// $this->call('UserTableSeeder');

這一句為

$this->call('PageTableSeeder');

然后運(yùn)行命令進(jìn)行數(shù)據(jù)填充:

composer dump-autoloadphp artisan db:seed

去看看 pages 表,是不是多了十行數(shù)據(jù)?

本教程示例代碼見:https://github.com/johnlui/Learn-Laravel-5

大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。

以上所述就是本文的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹覍W(xué)習(xí)Laravel5框架有所幫助。

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 create Laravel package (Package) development? How to create Laravel package (Package) development? May 29, 2025 pm 09:12 PM

The steps to create a package in Laravel include: 1) Understanding the advantages of packages, such as modularity and reuse; 2) following Laravel naming and structural specifications; 3) creating a service provider using artisan command; 4) publishing configuration files correctly; 5) managing version control and publishing to Packagist; 6) performing rigorous testing; 7) writing detailed documentation; 8) ensuring compatibility with different Laravel versions.

What is Middleware in Laravel? How to use it? What is Middleware in Laravel? How to use it? May 29, 2025 pm 09:27 PM

Middleware is a filtering mechanism in Laravel that is used to intercept and process HTTP requests. Use steps: 1. Create middleware: Use the command "phpartisanmake:middlewareCheckRole". 2. Define processing logic: Write specific logic in the generated file. 3. Register middleware: Add middleware in Kernel.php. 4. Use middleware: Apply middleware in routing definition.

Laravel Page Cache Policy Laravel Page Cache Policy May 29, 2025 pm 09:15 PM

Laravel's page caching strategy can significantly improve website performance. 1) Use cache helper functions to implement page caching, such as the Cache::remember method. 2) Select the appropriate cache backend, such as Redis. 3) Pay attention to data consistency issues, and you can use fine-grained caches or event listeners to clear the cache. 4) Further optimization is combined with routing cache, view cache and cache tags. By rationally applying these strategies, website performance can be effectively improved.

Laravel MVC Architecture: what can go wrong? Laravel MVC Architecture: what can go wrong? Jun 05, 2025 am 12:05 AM

Laravel'sMVCarchitecturecanfaceseveralissues:1)Fatcontrollerscanbeavoidedbydelegatinglogictoservices.2)Overloadedmodelsshouldfocusondataaccess.3)Viewsshouldremainsimple,avoidingPHPlogic.4)PerformanceissueslikeN 1queriescanbemitigatedwitheagerloading.

How to populate test data using Seeder in Laravel? How to populate test data using Seeder in Laravel? May 29, 2025 pm 09:21 PM

Using Seeder to fill test data in Laravel is a very practical trick in the development process. Below I will explain in detail how to achieve this, and share some problems and solutions I encountered in actual projects. In Laravel, Seeder is a tool used to populate databases. It can help us quickly generate test data, which facilitates development and testing. Using Seeder not only saves time, but also ensures data consistency, which is especially important for team collaboration and automated testing. I remember that in a project, we needed to generate a large amount of product and user data for an e-commerce platform, and Seeder came in handy at that time. Let's see how to use it. First, make sure your Lara is

What is Laravel Migrations? How to use it? What is Laravel Migrations? How to use it? May 29, 2025 pm 09:24 PM

Laravel's migration is a database version control tool that allows developers to programmatically define and manage database structure changes. 1. Create a migration file using the Artisan command. 2. The migration file contains up and down methods, which defines the creation/modification and rollback of database tables respectively. 3. Use the phpartisanmigrate command to execute the migration, and use phpartisanmigrate:rollback to rollback.

Laravel: Simple MVC project for beginners Laravel: Simple MVC project for beginners Jun 08, 2025 am 12:07 AM

Laravel is suitable for beginners to create MVC projects. 1) Install Laravel: Use composercreate-project--prefer-distlaravel/laravelyour-project-name command. 2) Create models, controllers and views: Define Post models, write PostController processing logic, create index and create views to display and add posts. 3) Set up routing: Configure/posts-related routes in routes/web.php. With these steps, you can build a simple blog application and master the basics of Laravel and MVC.

What are policies in Laravel, and how are they used? What are policies in Laravel, and how are they used? Jun 21, 2025 am 12:21 AM

InLaravel,policiesorganizeauthorizationlogicformodelactions.1.Policiesareclasseswithmethodslikeview,create,update,anddeletethatreturntrueorfalsebasedonuserpermissions.2.Toregisterapolicy,mapthemodeltoitspolicyinthe$policiesarrayofAuthServiceProvider.

See all articles