laravel基础知识学习总结(路由、中间件、控制器)

 路由:

  • 路由:是将信息从源地址传递到目的地得角色
  • Route::get('/test',function(){ });   //分号不能丢
  • 报错信息MethodNotAllowedHttpException in RouteController.php 说明当前得请求方式和路由规则不匹配
  • laravel框架中所有得模板文件都是存放在resources/views中
  • 模板文件得后缀名是 .blade.php
  • 错误信息TokenMismatchException in VerifyCsrfToken.php 说明当前得post请求中缺少验证信息.在每一个post表单中需要添加一个token
  • 当前路由规则中的参数名可以和匿名函数或者方法中的参数名,不保持一致.潜规则保持一致

routes.php

 <?php
//基本路由
Route::get('/',function(){
return view('welcome');
});
//路由组
Route::group([],function(){
//带参数的路由
Route::get('/user/{id}',function($ids){
echo "当前用户的id为".$ids;
}); //阻止参数的类型
Route::get('/goods/{id}',function($id){
echo "商品的详情页..当前id为".$id;
})->where('id','\d+'); //多个参数
Route::get('/{type}-{id}',function ($type,$id){ //该处的参数一定要传,不然下面无法使用这个参数
echo "我的类型是".$type."我的id是".$id;
}); //路由别名
Route::get('Admin/User/index',[
'as'=>'bieming', //as和uses是固定的,不可修改
'uses'=>function(){
echo route('bieming'); //route是一个函数,通过路由别名来快速创建完整的url
}
]); //404页面
Route::get('/404',function(){
// -------------------方式1-------------------
// abort(404);
// -------------------方式2-------------------
return view('errors.404');
});
});

中间件

中间件:过滤请求

  • 创建:php artisan make:middleware TestMiddelware
  • 默认存放目录:app\http\middelware中
  • $request是laravel框架对请求报文做的封装

使用:

  1. 创建中间件
    //创建中间件(文件位置:App\Http\Middleware)
    php artisan make:middleware LoginMiddleware
  2. 注册
    //注册分为全局注册和路由注册(位置:App\Http\Kernel.php)
    // 设置全局注册
    protected $middleware = [
    \App\Http\Middleware\TestMiddelware::class, //该句打印出来的是TestMiddelware的路径(App\Http\Middleware\TestMiddelware)
    ]; // 设置路由注册
    protected $routeMiddleware = [
    'login'=>\App\Http\Middleware\LoginMiddleware::class,
    ];
  3. 使用(全局注册不需要该步,因为全局注册是针对所有的请求)
    <?php
    //方式1
    Route::get('/setting',[
    'middleware'=>'login',
    'uses'=>function(){
    echo "这是设置页面。。。。";
    }
    ]);
    //方式2
    Route::get('/admin',function(){
    echo "这是网站的后台";
    })->middleware('login');
    }); 

示例代码:

App\Http\kernel.php

 <?php

 namespace App\Http;

 use Illuminate\Foundation\Http\Kernel as HttpKernel;

 class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
// 设置全局注册
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\TestMiddelware::class, //该句打印出来的是TestMiddelware的路径(App\Http\Middleware\TestMiddelware)
]; /**
* The application's route middleware.
*
* @var array
*/
// 设置路由注册
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'login'=>\App\Http\Middleware\LoginMiddleware::class,
];
}

app\Http\Middleware\TestMiddelware.php

 <?php

 namespace App\Http\Middleware;

 use Closure;

 class TestMiddelware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// 记录该请求的路径
// [2018-08-08 08:08:08]127.0.0.1-------/Admin/User/index
$path='['.date('Y-m-d H:i:s') .']'.$request->ip().'-------'.$request->path();
// 将字符串路径存放到日志中
file_put_contents('request.log',$path."\r\n",FILE_APPEND);
// 进入下一层的代码执行
return $next($request);
}
}

app\Http\Middleware\LoginMiddleware.php

 <?php

 namespace App\Http\Middleware;

 use Closure;

 class LoginMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// 检测请求是否具有session信息
if (!session('uid')) {
//跳转
return redirect('/login');
}
return $next($request);
}
}

route.php

 <?php
Route::get('/setting',[
'middleware'=>'login',
'uses'=>function(){
echo "这是设置页面。。。。";
}
]);
Route::get('/session',function(){
session(['uid'=>10]);
});
Route::get('/login',function(){
echo "这里是用户的登陆页面";
});
//网站后台
Route::get('/admin',function(){
echo "这是网站的后台";
})->middleware('login');
});

 控制器

  • 概念:控制model和view
  • 创建控制器
    //方式1
    php artisan make:controller UserController
    //方式2.创建一个空控制器
    php artisan make:controller TestController --plain(不创建类方法) 

资源控制器的访问规则:

Verb    Path                        Action  Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT /users/{user} update users.update
DELETE /users/{user} destroy users.destroy

隐式控制器和资源控制器示例代码:

route.php

<?php
//隐式控制器
Route::controller('cmftest','TestController');
//资源控制器
Route::resource('user','UserController');
});

TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller; class TestController extends Controller
{
// 函数名称必须为”method+访问名称“
// 访问名称的第一次字母大写
// 访问地址:localhost:10/cmftest/test
public function getTest()
{
return "这是test函数";
}
}

UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller; class UserController extends Controller
{
//访问路径:http://localhost:10/user
public function index()
{
echo "index";
}
//访问路径:http://localhost:10/user/create
public function create()
{
echo "create";
}
}

脑图总结: laravel基础知识学习总结(路由、中间件、控制器)

脑图地址:http://naotu.baidu.com/file/f7317bc2f0f514db9e996b7705255910?token=b53e299981abe1da

laravel基础知识学习总结(路由、中间件、控制器)

上一篇:Linux----------mysql基础


下一篇:win7下硬盘安装Windows