跟我学Laravel之快速入门
本文是本系列教程中的第一篇,主要介绍一些 Laravel 基础部分,但是还有更让人兴奋的东西要学。这是后面文章我们要进一步深入的
安装
Laravel框架使用 Composer 执行安装和依赖管理。如果还没有安装的话,现在就开始 安装 Composer 吧。
安装Composer之后,你就可以通过命令行使用如下命令安装Laravel了:
composer create-project laravel/laravel your-project-name
或者,你可以从 Github仓库 下载。接下来,在 安装Composer 之后,在项目根目录下执行 composer install 命令。该命令将会下载以及安装框架的依赖组件。
写入权限
安装完 Laravel ,你还需要为web服务器设置 app/storage 目录的写入权限。请参考 安装 一节以获取更多关于配置方面的信息。
目录结构
安装完框架后,你需要熟悉一下该项目的目录结构。app 文件夹包含了一些例如 views ,controllers 和 models 目录。 程序中大部分代码将要存放这些目录下。你也可以查看一下 app/config 文件夹里一些配置项目。
路由
我们开始创建我们第一个路由。在 Laravel,简单路由的方法是闭包。打开 app/routes.php 文件加入如下代码:
Route::get('users', function()
{
return 'Users!';
});
现在,你在 web 浏览器输入 /users,你应该会看到 Users! 输出。真棒!已经创建了你第一个路由。
路由也可以赋予控制器类。例如:
Route::get('users', 'UserController@getIndex');
该路由告知框架 /users 路由请求应该调用 UserController 类的 getIndex 方法。要查看更多关于路由控制器信息,查看 控制器文档 。
创建视图
接下来,我们要创建视图来显示我们用户数据。视图以HTML代码存放在 app/views 文件夹。我们将存放两个视图文件到该文件夹:layout.blade.php 和 users.blade.php。首先,让我们先创建 layout.blade.php 文件:
复制代码 代码如下:
Laravel Quickstart
@yield('content')
接着, 我们创建 users.blade.php 视图:
复制代码 代码如下:
@extends('layout')
@section('content')
Users!
@stop
这里的语法可能让你感到陌生。因为我们使用的是 Laravel 模板系统:Blade。Blade 非常快,因为仅使用了少量的正则表达式来为你的模板编译成原始PHP代码。Blade提供强大的功能,例如模板继承,还有一些常用的PHP控制结构语法糖,例如 if 和 for。 查看 Blade 文档 了解更多。
现在我们有了我们视图,让我们返回 /users 路由。我们用视图来替代返回 Users!:
复制代码 代码如下:
Route::get('users', function()
{
return View::make('users');
});
漂亮!现在你成功创建了继承至layout的视图。接下来,让我们开始数据库层。
创建迁移
要创建表来保存我们数据,我们将使用 Laravel 迁移系统。迁移描述数据库的改变,这让分享给他们团队成员非常简单。
首先,我们配置数据库连接。你可以在 app/config/database.php 文件配置所有数据库连接信息。默认,Laravel 被配置为使用 SQLite,并且一个 SQLite 数据库存放在 app/database 目录。你可以将数据库配置文件的 driver 选项修改为 mysql 并且配置 mysql 连接信息。
接下来,要创建迁移,我们将使用 Artisan CLI。在项目根目录中,在终端中执行以下命令:
复制代码 代码如下:
php artisan migrate:make create_users_table
然后,找到生成的迁移文件 app/database/migrations 目录。该文件包含了一个包含两个方法: up 和 down 的类。在 up 方法,你要指名数据库表的修改,在 down 方法中你只需要移除它。
让我们定义如下迁移:
复制代码 代码如下:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
然后,我们在项目根目录中使用终端运行 migrate 命令来执行迁移:
复制代码 代码如下:
php artisan migrate
如果你想回滚迁移,你可以执行 migrate:rollback 命令。现在我们已经有了数据库表,让我们让添加一些数据!
Eloquent ORM
Laravel 提供非常棒的 ORM:Eloquent。如果你使用过 Ruby on Rails 框架,你会发现 Eloquent 很相似,因为它遵循数据库交互的 ActiveRecord ORM 风格。
首先,让我们来定义个模型。ELoquent 模型可以用来查询相关数据表,,以及表内的某一行。别着急,我们很快会谈及!模型通常存放在 app/models 目录。让我们在该目录定义个 User.php 模型,如:
复制代码 代码如下:
class User extends Eloquent {}
注意我们并没有告诉 Eloquent 使用哪个表。Eloquent 有多种约定, 一个是使用模型的复数形式作为模型的数据库表。非常方便!
使用你喜欢的数据库管理工具,插入几行数据到 users 表,我们将使用 Eloquent 取得它们并传递到视图中。
现在我们修改我们 /users 路由如下:
复制代码 代码如下:
Route::get('users', function()
{
$users = User::all();
return View::make('users')->with('users', $users);
});
让我们来看看该路由。首先,User 模型的 all 方法将会从 users 表中取得所有记录。接下来,我们通过 with 方法将这些记录传递到视图。with 方法接受一个键和一个值,那么该值就可以在视图中使用了。
激动啊。现在我们准备将用户显示在我们视图!
显示数据
现在我们视图中已经可以访问 users 类,我们可以如下显示它们:
复制代码 代码如下:
@extends('layout')

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

In the Laravel framework version selection guide for beginners, this article dives into the version differences of Laravel, designed to assist beginners in making informed choices among many versions. We will focus on the key features of each release, compare their pros and cons, and provide useful advice to help beginners choose the most suitable version of Laravel based on their skill level and project requirements. For beginners, choosing a suitable version of Laravel is crucial because it can significantly impact their learning curve and overall development experience.

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.
