沃梦达 / 编程问答 / php问题 / 正文

在 Laravel 5 中扩展请求类

Extend Request class in Laravel 5(在 Laravel 5 中扩展请求类)

本文介绍了在 Laravel 5 中扩展请求类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Laravel 的新手(只体验过 Laravel 5,所以这里没有遗留问题)

我想知道如何扩展核心请求类.除了如何扩展它之外,我想知道这样做是否是一个明智的设计决定.

我已经广泛阅读了文档(特别是关于注册服务提供者以及它提供 Facades 访问依赖项容器内的条目的方式) - 但我可以看到(并找到)没有办法替换 IlluminateHttpRequest 实例与我自己的

解决方案

这里是官方文档:Request Lifecycle >

app/Http/CustomRequest.php 的内容

将此行添加到 public/index.php

$app->alias('request', 'AppHttpCustomRequest');

之后

app = require_once __DIR__.'/../bootstrap/app.php';

更改 public/index.php 中的代码

IlluminateHttpRequest::capture()

AppHttpCustomRequest::capture()

I'm new to Laravel (only experienced Laravel 5, so no legacy hang up here)

I'd like to know how to extend the core Request class. In addition to how to extend it, i'd like to know if it's a wise design decision to do so.

I've read through the documentation extensively (especially with regards to registering service providers and the manner in which it provides Facades access to entries within the dependency container) - but I can see (and find) no way to replace the IlluminateHttpRequest instance with my own

解决方案

Here is Official Document: Request Lifecycle

Content of app/Http/CustomRequest.php

<?php namespace AppHttp;

use IlluminateHttpRequest as BaseRequest;

class CustomRequest extends BaseRequest {
    // coding
}

add this line to public/index.php

$app->alias('request', 'AppHttpCustomRequest');

after

app = require_once __DIR__.'/../bootstrap/app.php';

change the code at public/index.php

IlluminateHttpRequest::capture()

to

AppHttpCustomRequest::capture()

这篇关于在 Laravel 5 中扩展请求类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Laravel 5 中扩展请求类

基础教程推荐