了解PHP/Laravel中的Constructor,$ this关键字和控制器类 [英] Understanding Constructor, $this keyword, and controller class in PHP/Laravel

查看:56
本文介绍了了解PHP/Laravel中的Constructor,$ this关键字和控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以前已经对此进行了介绍,但是我正在努力将其应用于我的代码中.

I know this has been covered in pieces before, but I am struggling with how to apply it to my code.

我正在Laravel中开发一个PHP应用程序,但想使我的代码更具模块化和可测试性,这意味着将我的逻辑从我的重型控制器中分离出来,并将其分离到单独的文件中,然后从控制器中调用它们.

I am developing a PHP application in Laravel but want to make my code more modular and testable, which means pulling my logic away from my heavyset controllers and separating them out into separate files and calling them from within the controller.

在一个这样的控制器( ImageController )中,我正在调用 ImageRepository.php 中的逻辑,它看起来像这样:

In one such controller (ImageController), I am calling upon the logic in ImageRepository.php and it looks like this:

    <?php
namespace App\Http\Controllers;
use App\Logic\Image\ImageRepository;
use Illuminate\Support\Facades\Input;
class ImageController extends Controller
{
    protected $image;
    public function __construct(ImageRepository $imageRepository)
    {
        $this->image = $imageRepository;
    }
    public function getUpload()
    {
        return view('pages.upload');
    }
    public function postUpload()
    {
        $photo = Input::all();
        $response = $this->image->upload($photo);
        return $response;
    }
    /*public function deleteUpload()
    {
        $filename = Input::get('id');
        if(!$filename)
        {
            return 0;
        }
        $response = $this->image->delete( $filename );
        return $response;
    }
    */
}

我的问题是我不了解此代码的工作原理,因为我是从另一个来源获得的,并且想了解它,因此我可以在代码中的其他地方复制此体系结构.

My problem is I don't understand how this code works, as I got it from another source and want to understand it so I can replicate this architecture elsewhere in my code.

我上传图片时的路线是:

My route when uploading an image is this:

Route::post('upload_image', ['as' => 'upload-post', 'uses'     =>'ImageController@postUpload']);

所以我的第一个问题是,我从来没有在路由中调用构造函数.转到 postUpload().这是否意味着没有目的?另外,为什么构造函数在 ImageRepository $ imageRepository 之间没有逗号...根据我对文档的理解,只有在其中一个是布尔值的情况下,您才这样做?

So my first question is, I never call the construct function in my route. It goes right to postUpload(). Does this mean it has no purpose? Also why does the construct function not have a comma between ImageRepository and $imageRepository... from my understanding of the docs you only do that if one of them is a boolean?

为什么 $ response = $ this-> image-> upload($ photo); 意味着 postUpload()中的任何内容?该功能 upload()来自存储库,使用足以使它知道该怎么做?为什么 $ this-> image 意味着什么, $ this 指的是什么? ImageController 类? $ this-> image 中的 image 是从受保护的$ image 衍生而来的吗?

Also why does $response = $this->image->upload($photo); mean anything in postUpload()? That function upload() comes from the Repository, is use enough so that it knows what to do? Why does $this->image mean anything, what does the $this refer to? The ImageController class? Is the image in $this->image derived from protected $image?

我想我应该先使用常规的PHP,然后再转向Laravel框架,因为尽管我可以轻松地浏览Laravel来制作一个正常的应用程序,但这似乎阻碍了我遵循最佳实践/体系结构的能力.AFAIK控制器主要是用来处理数据并将其发送到视图或数据库的东西,也许我不明白为什么它是一类?

I guess I should have stuck with regular PHP before moving to the Laravel framework because while I can navigate through Laravel easily enough to make a working application, it seems to be hindering my ability to follow best practices / architecture. AFAIK a controller is mainly something to manipulate data and send it to the view or the database, I don't understand perhaps why it is a class?

很抱歉,我很困惑.我在codeacademy上自学了php,但是它们的类声明和对象实例很容易理解,事实并非如此.如果有人可以向我解释代码,那将非常有帮助.

Sorry for the multiple questions I am just very confused. I taught myself php on codeacademy but their class declarations and object instances are very easy to follow, this is not. If someone could explain the code to me that would be very helpful.

谢谢!

推荐答案

我从来没有在路由中调用构造函数.正确的方法是postUpload().这是否意味着它没有目的?

I never call the construct function in my route. It goes right to postUpload(). Does this mean it has no purpose?

构造器是创建控制器对象时会自动调用.

The constructor is automatically called when the controller object is created.

为什么构造函数在ImageRepository和$ imageRepository之间没有逗号...根据我对文档的理解,只有在其中一个是布尔值的情况下,您才这样做?

why does the construct function not have a comma between ImageRepository and $imageRepository... from my understanding of the docs you only do that if one of them is a boolean?

ImageRepository 不是另一个参数.它是类型提示(或在PHP 7,类型为声明)

ImageRepository is not another argument. It is a type hint (or in PHP 7, a type declaration)

为什么$ response = $ this-> image-> upload($ photo);什么意思在postUpload()中?该功能upload()来自存储库,足够使用了,它知道该怎么办?

why does $response = $this->image->upload($photo); mean anything in postUpload()? That function upload() comes from the Repository, is use enough so that it knows what to do?

要使控制器能够使用 ImageRepository ,必须使用 use .使用 $ this-> image = $ imageRepository; 将其加载到构造函数中的对象后,您的控制器方法就可以访问其方法(例如 upload )通过 $ this-> image .

The use is necessary for your controller to be able to use ImageRepository. Once it is loaded into your object in the constructor with $this->image = $imageRepository;, your controller methods have access to its methods, (such as upload) via $this->image.

为什么$ this-> image表示什么,$ this表示什么?

Why does $this->image mean anything, what does the $this refer to?

是的, $ this 确实是指 ImageController 类. PHP手册指出:

Yes, $this does refer to the ImageController class. The PHP manual states:

从对象上下文中调用方法时,伪变量$ this可用.$ this是对调用对象的引用


$ this-> image中的图像是否源自受保护的$ image?

Is the image in $this->image derived from protected $image?

受保护的$ image 设置可见性 $ image 属性的a>.它不会为该属性分配任何内容.这是在构造函数中完成的.

protected $image sets the visibility of the controller object's $image property. It does not assign anything to the property; that is done in the constructor.

这篇关于了解PHP/Laravel中的Constructor,$ this关键字和控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆