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.
Video playback address: //m.sbmmt.com/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 routingThe 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
Route::get('/', function(){return 'Hello World';});
Route::post('foo/bar', function() { return 'Hello World'; });
Register a route to respond to all HTTP methods
Route::any('foo', function() { return 'Hello World'; });
Force a route to be accessed through HTTPS
Route::get('foo', array('https', function() { return 'Must be over HTTPS'; }));
Often you need to generate URLs based on routes. You can do this by using the URL::to method:
$url = URL::to('foo');
Route::get('user/{id}', function($id) { return 'User '.$id; });
The optional routing parameter
Route::get('user/{name?}', function($name = null) { return $name; });
Optional routing parameters with default values
Route::get('user/{name?}', function($name = 'John') { return $name; });
Routing with regular expression constraints
Route::get('user/{name}', function($name) { // }) ->where('name', '[A-Za-z]+'); Route::get('user/{id}', function($id) { // }) ->where('id', '[0-9]+');
Route::filter('old', function() { if (Input::get('age') < 200) { return Redirect::to('home'); } });
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.
Route::get('user', array('before' => 'old', function() { return 'You are over 200 years old!'; }));
Specify multiple routing filters for a route
Route::get('user', array('before' => 'auth|old', function() { return 'You are authenticated and over 200 years old!'; }));
Specify routing filter parameters
Route::filter('age', function($route, $request, $value) { // }); Route::get('user', array('before' => 'age:200', function() { return 'Hello World'; }));
When the routing filter receives the third The response of the parameters $response:
Route::filter('log', function($route, $request, $response, $value) { // });
Basic routing filter pattern
Route::filter('admin', function() { // }); Route::when('admin/*', 'admin');
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.
Route::when('admin/*', 'admin', array('post'));
class FooFilter { public function filter() { // Filter logic... } }
Register a class-based filter
Route::filter('foo', 'FooFilter');
Route::get('user/profile', array('as' => 'profile', function() { // }));
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));
$url = URL::route('profile'); $redirect = Redirect::route('profile');
$name = Route::currentRouteName();
Route::group(array('before' => 'auth'), function() { Route::get('/', function() { // Has Auth Filter }); Route::get('user/profile', function() { // Has Auth Filter }); });
Route::group(array('domain' => '{account}.myapp.com'), function() { Route::get('user/{id}', function($account, $id) { // }); });
Route::group(array('prefix' => 'admin'), function() { Route::get('user', function() { // }); });
模型绑定提供了一个简单的方法向路由中注入模型。比如,不仅注入一个用户的 ID,您可以根据指定的 ID 注入整个用户模型实例。首先使用 Route::model 方法指定所需要的模型:
为模型绑定一个变量
代码如下:
Route::model('user', 'User');
然后, 定义一个包含 {user} 参数的路由:
代码如下:
Route::get('profile/{user}', function(User $user) { // });
因为我们已经绑定 {user} 参数到 User 模型,一个 User 实例将被注入到路由中。因此,比如一个 profile/1 的请求将注入一个 ID 为 1 的 User 实例。
注意: 如果在数据库中没有找到这个模型实例,将引发404错误。
如果您希望指定您自己定义的没有找到的行为,您可以为 model 方法传递一个闭包作为第三个参数:
代码如下:
Route::model('user', 'User', function() { throw new NotFoundException; });
有时您希望使用自己的方法处理路由参数,可以使用 Route::bind 方法:
代码如下:
Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); });
引发404错误
有两种方法在路由中手动触发一个404错误。首先,您可以使用 App::abort 方法:
代码如下:
App::abort(404);
其次,您可以抛出一个 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!