Home Backend Development PHP Tutorial Resource sharing about building a complete blog system using the Yii2 framework

Resource sharing about building a complete blog system using the Yii2 framework

Sep 01, 2017 am 09:49 AM
yii2 blog whole

Yii is one of the best practices for rapid development of PHP. With its rich expansion resources and rapid development ideas, it is increasingly favored by enterprises and has become more widely used. This course takes the blog system as an example to describe how to use yii2.0 for practical development and learn the practical application of yii2.0. The content is divided into three parts: basic configuration, blog front-end, and blog back-end.

Resource sharing about building a complete blog system using the Yii2 framework

Video playback address: http://www.php.cn/course/266.html

The teacher’s teaching style:

The teacher’s lectures are simple, clear, layer-by-layer analysis, interlocking, rigorous argumentation, rigorous structure, and use the logical power of thinking to attract students’ attention Strength, use reason to control the classroom teaching process. By listening to the teacher's lectures, students not only learn knowledge, but also receive training in thinking, and are also influenced and influenced by the teacher's rigorous academic attitude

The more difficult point in this video is the framework of the Yii framework blog system Routes configured:

##Basic routing

The vast majority of routes for your application will be in the app/routes.PHP file defined in. The simplest route in Laravel consists of a URI and a closure call.

Basic GET routing


The code is as follows:

Route::get('/', function(){return 'Hello World';});
Copy after login

Basic POST routing


The code is as follows:

Route::post('foo/bar', function()
{
return 'Hello World';
});
Copy after login

Register a route to respond to all HTTP methods

The code is as follows:

Route::any('foo', function()
{
   return 'Hello World';
});
Copy after login

Force a route to be accessed through HTTPS

Code As follows:

Route::get('foo', array('https', function()
{
    return 'Must be over HTTPS';
}));
Copy after login

Often you need to generate URLs based on routes. You can do this by using the URL::to method:

The code is as follows:

$url = URL::to('foo');
Copy after login

Route Parameter


The code is as follows:

Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
Copy after login

The optional routing parameter

The code is as follows:

Route::get('user/{name?}', function($name = null)
{
return $name;
});
Copy after login

Optional routing parameters with default values

The code is as follows:

Route::get('user/{name?}', function($name = 'John')
{
return $name;
});
Copy after login

Routing with regular expression constraints

The code is as follows:

Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');
Copy after login

Route Filter

Route filter provides a simple way to restrict access to specified routes, which is very useful when you need to create a zone for your site that requires authentication. The Laravel framework contains some routing filters, such as auth filter, auth.basic filter, guest filter, and csrf filter. They are stored in the app/filters.php file.

Define a routing filter


The code is as follows:

Route::filter('old', function()
{
if (Input::get(&#39;age&#39;) < 200)
{
return Redirect::to(&#39;home&#39;);
}
});
Copy after login

If a response is returned from a routing filter, the response is considered In response to this request, the route will not be executed, and any after filters related to this route will also be canceled.

Specify a routing filter for a route


The code is as follows:

Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;old&#39;, function()
{
return &#39;You are over 200 years old!&#39;;
}));
Copy after login

Specify multiple routing filters for a route

The code is as follows:

Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;auth|old&#39;, function()
{
return &#39;You are authenticated and over 200 years old!&#39;;
}));
Copy after login

Specify routing filter parameters

The code is as follows:

Route::filter(&#39;age&#39;, function($route, $request, $value)
{
//
});
Route::get(&#39;user&#39;, array(&#39;before&#39; => &#39;age:200&#39;, function()
{
return &#39;Hello World&#39;;
}));
Copy after login

When the routing filter receives the third The response of the parameters $response:

The code is as follows:

Route::filter(&#39;log&#39;, function($route, $request, $response, $value)
{
//
});
Copy after login

Basic routing filter pattern

You may want to assign a group of routes based on the URI Specify filter.


The code is as follows:

Route::filter(&#39;admin&#39;, function()
{
//
});
Route::when(&#39;admin/*&#39;, &#39;admin&#39;);
Copy after login

In the above example, the admin filter will be applied with all routes starting with admin/. The asterisk acts as a wildcard character and will match all character combinations.

You can also constrain the pattern filter by specifying the HTTP method:

The code is as follows:

Route::when(&#39;admin/*&#39;, &#39;admin&#39;, array(&#39;post&#39;));
Copy after login

Filter class

For advanced filters, you A class can be used instead of a closure function. Because the filter class is an IoC container that lives outside the application, you can use dependency injection in the filter, making it easier to test.

Define a filter class


The code is as follows:

class FooFilter {
public function filter()
{
// Filter logic...
}
}
Copy after login

Register a class-based filter

Code As follows:

Route::filter(&#39;foo&#39;, &#39;FooFilter&#39;);
Copy after login

Named routing

Named routing makes it easier to specify routes when generating redirects or URLs. You can specify a name for the route like this:


The code is as follows:

Route::get(&#39;user/profile&#39;, array(&#39;as&#39; => &#39;profile&#39;, function()
{
//
}));
Copy after login

You can also specify a route name for the controller method:


The code is as follows:

Route::get(&#39;user/profile&#39;, array(&#39;as&#39; => &#39;profile&#39;, &#39;uses&#39; => &#39;UserController@showProfile&#39;));
Copy after login

Now you use the route name when generating URLs or jumps:


The code is as follows:

$url = URL::route(&#39;profile&#39;);
$redirect = Redirect::route(&#39;profile&#39;);
Copy after login

You can use the currentRouteName method to get it The name of a route:

The code is as follows:

$name = Route::currentRouteName();
Copy after login

Route group

Sometimes you may want to apply a filter to a group of routes. You do not need to specify filters for each route, you can use route groups:


The code is as follows:

Route::group(array(&#39;before&#39; => &#39;auth&#39;), function()
{
Route::get(&#39;/&#39;, function()
{
// Has Auth Filter
});
Route::get(&#39;user/profile&#39;, function()
{
// Has Auth Filter
});
});
Copy after login

Subdomain routing

Laravel routing can also handle wildcards subdomain name, and obtain wildcard parameters from the domain name:

Register subdomain name routing


The code is as follows:

Route::group(array(&#39;domain&#39; => &#39;{account}.myapp.com&#39;), function()
{
Route::get(&#39;user/{id}&#39;, function($account, $id)
{
//
});
});
Copy after login

Routing prefix

One Group routing can add a prefix to the routing group by using the prefix option in the attribute array:

Add a prefix to the routing group


The code is as follows:

Route::group(array(&#39;prefix&#39; => &#39;admin&#39;), function()
{
Route::get(&#39;user&#39;, function()
{
//
});
});
Copy after login

Routing model binding Certainly

  模型绑定提供了一个简单的方法向路由中注入模型。比如,不仅注入一个用户的 ID,您可以根据指定的 ID 注入整个用户模型实例。首先使用 Route::model 方法指定所需要的模型:

为模型绑定一个变量

代码如下:

Route::model(&#39;user&#39;, &#39;User&#39;);
Copy after login


然后, 定义一个包含 {user} 参数的路由:

代码如下:

Route::get(&#39;profile/{user}&#39;, function(User $user)
{
//
});
Copy after login

  因为我们已经绑定 {user} 参数到 User 模型,一个 User 实例将被注入到路由中。因此,比如一个 profile/1 的请求将注入一个 ID 为 1 的 User 实例。

  注意: 如果在数据库中没有找到这个模型实例,将引发404错误。

  如果您希望指定您自己定义的没有找到的行为,您可以为 model 方法传递一个闭包作为第三个参数:

代码如下:

Route::model(&#39;user&#39;, &#39;User&#39;, function()
{
throw new NotFoundException;
});
Copy after login


  有时您希望使用自己的方法处理路由参数,可以使用 Route::bind 方法:

代码如下:

Route::bind(&#39;user&#39;, function($value, $route)
{
return User::where(&#39;name&#39;, $value)->first();
});
Copy after login


引发404错误

  有两种方法在路由中手动触发一个404错误。首先,您可以使用 App::abort 方法:

代码如下:

App::abort(404);
Copy after login

其次,您可以抛出一个 Symfony\Component\HttpKernel\Exception\NotFoundHttpException 的实例。

更多关于处理404异常和为这些错误使用使用自定义响应的信息可以在 错误 章节中找到。

路由至控制器

Laravel 不仅允许您路由至闭包,也可以路由至控制器类,甚至允许创建 资源控制器.

The above is the detailed content of Resource sharing about building a complete blog system using the Yii2 framework. For more information, please follow other related articles on the PHP Chinese website!

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)

Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Feb 19, 2024 pm 04:01 PM

Starting from scratch, I will teach you step by step how to install Flask and quickly build a personal blog. As a person who likes writing, it is very important to have a personal blog. As a lightweight Python Web framework, Flask can help us quickly build a simple and fully functional personal blog. In this article, I will start from scratch and teach you step by step how to install Flask and quickly build a personal blog. Step 1: Install Python and pip Before starting, we need to install Python and pi first

What are the top ten open source PHP blog systems in 2022? 【recommend】 What are the top ten open source PHP blog systems in 2022? 【recommend】 Jul 27, 2022 pm 05:38 PM

Blog, also translated as web log, blog or blog, is a website that is usually managed by individuals and posts new articles from time to time. So how to set up a blog? What are the PHP blog systems? Which blogging system is best to use? Below, PHP Chinese website will summarize and share the top ten open source PHP blog systems with you. Let’s take a look!

Create a simple blog: using PHP and SQLite Create a simple blog: using PHP and SQLite Jun 21, 2023 pm 01:23 PM

With the development of the Internet, blogs have become a platform for more and more people to share their lives, knowledge and ideas. If you also want to create a blog of your own, then this article will introduce how to use PHP and SQLite to create a simple blog. Determine the needs Before starting to create a blog, we need to determine the functions we want to achieve. For example: Create a blog post Edit a blog post Delete a blog post Display a list of blog posts Display blog post details User authentication and permission control Install PHP and SQLite We need to install PHP and S

How to remove jquery in yii2 How to remove jquery in yii2 Feb 17, 2023 am 09:55 AM

How to remove jquery from yii2: 1. Edit the AppAsset.php file and comment out the "yii\web\YiiAsset" value in the variable $depends; 2. Edit the main.php file and add the configuration "'yii" under the field "components" \web\JqueryAsset' => ['js' => [],'sourcePath' => null,]," to remove the jquery script.

Build a blog website using the Python Django framework Build a blog website using the Python Django framework Jun 17, 2023 pm 03:37 PM

With the popularity of the Internet, blogs play an increasingly important role in information dissemination and communication. In this context, more and more people are starting to build their own blog sites. This article will introduce how to use the PythonDjango framework to build your own blog website. 1. Introduction to the PythonDjango framework PythonDjango is a free and open source web framework that can be used to quickly develop web applications. The framework provides developers with powerful tools to help them build feature-rich

How to create a simple blog using PHP How to create a simple blog using PHP Sep 24, 2023 am 08:25 AM

How to create a simple blog using PHP 1. Introduction With the rapid development of the Internet, blogs have become an important way for people to share experiences, record life and express opinions. This article will introduce how to use PHP to create a simple blog, with specific code examples. 2. Preparation Before starting, you need to have the following development environment: a computer with a PHP interpreter and Web server (such as Apache) installed, a database management system, such as MySQL, a text editor or IDE3

How to create a blog How to create a blog Oct 10, 2023 am 09:46 AM

You can create a blog by determining the topic and target audience of the blog, choosing a suitable blogging platform, registering a domain name and purchasing hosting, designing the appearance and layout of the blog, writing quality content, promoting the blog, and analyzing and improving it.

A few selected CTF exercises will help you learn the yii2 framework! A few selected CTF exercises will help you learn the yii2 framework! Feb 23, 2022 am 10:33 AM

This article will introduce you to the yii2 framework, share a few CTF exercises, and use them to learn the yii2 framework. I hope it will be helpful to everyone.

See all articles