简体   繁体   English

将控制器添加到 Laravel 5 包

[英]Add a Controller to a Laravel 5 Package

Lately, I've been creating only models for my L5 packages, but now I'd like to try and introduce a Controller as well.最近,我一直只为我的 L5 包创建模型,但现在我也想尝试引入一个控制器。 I've been following this but I always get the error "Class StatController does not exist" .我一直在关注这个,但我总是收到错误"Class StatController does not exist"

Folder structure文件夹结构

/src
    routes.php
    /Controllers
        StatController.php

StatController.php状态控制器.php

<?php namespace Enchance\Sentrysetup\Controllers;

use App\Http\Controllers\Controller;

class StatController extends Controller {

    public function index() {
        return 'Moot.';
    }

}

Service provider服务提供者

public function register()
{
    // Can't get this to work
    include __DIR__.'/routes.php';
    $this->app->make('Enchance\Sentrysetup\Controllers\StatController');

    $this->app['sentrysetup'] = $this->app->share(function($app) {
        return new Sentrysetup;
    });
}

routes.php路由文件

Route::get('slap', 'StatController@index');

Does anyone have an alternate way of assigning a Controller to a L5 package?有没有人有将控制器分配给 L5 包的替代方法?

You don't need to call $this->app->make() on your controllers.您不需要在控制器上调用$this->app->make() Controllers are resolved by Laravel's IoC automatically (meaning that Laravel creates / instantiates controllers automatically that are tied to routes).控制器由 Laravel 的 IoC 自动解析(意味着 Laravel 自动创建/实例化与路由绑定的控制器)。

Require your routes in the boot() method of your packages service provider:在您的包服务提供商的boot()方法中要求您的路由:

public function boot()
{
    require __DIR__.'/routes.php';     
}

And inside your routes.php file:在你的routes.php文件中:

Route::group(['namespace' => 'Enchance\Sentrysetup\Controllers'], function()
{
    Route::get('slap', ['uses' => 'StatController@index']);
})

Also, just a tip.另外,只是一个提示。 You should PascalCase your namespaces:你应该PascalCase你的命名空间:

Enchance\SentrySetup\Controllers

Note the capital S in setup注意设置中的大写 S

The boot() method should be used to register your routes because when Laravel starts up, it goes through each service provider in your config/app.php file, creates them, calls the register() method (to insert/add any dependencies that the service provider "provides" into Laravel's singleton / IoC container). boot()方法应该用于注册你的路由,因为当 Laravel 启动时,它会通过你的config/app.php文件中的每个服务提供者,创建它们,调用register()方法(插入/添加任何依赖服务提供者“提供”到 Laravel 的单例/IoC 容器中)。

Think of Laravel's container as simply a big key => value array.将 Laravel 的容器视为一个简单的大键 => 值数组。 Where the "key" is the name of the dependency (such as config ), and the "value" is a closure ( function () {} ) that is called to create the dependency:其中“键”是依赖项的名称(例如config ),而“值”是被调用以创建依赖项的闭包( function () {} ):

// Exists as a property inside the Container:
$bindings = [
    "config" => function () {
        // Create the application configuration.
    }
];

// Create the app configuration.
$config = $bindings['config']();

// Create the app configuration (Laravel).
$config = app()->make('config');

Once Laravel has registered each provider, it then goes through them again and calls the boot() method.一旦 Laravel 注册了每个提供者,它就会再次通过它们并调用boot()方法。 This ensures that any dependencies that have been registered (inside of the register() method of all of your application service providers) is available in the boot() method, ready to be used.这确保已注册的任何依赖项(在所有应用程序服务提供者的register()方法内)在boot()方法中可用,随时可用。

The Service Provider Boot Method - Laravel Docs服务提供者启动方法 - Laravel Docs

Use this in controller function:在控制器功能中使用它:

use Illuminate\Routing\Controller;

Like:喜欢:

namespace Enchance\Sentrysetup\Controllers;

use Illuminate\Routing\Controller;

class StatController extends Controller {

    public function index() {
        return 'Moot.';
    }

}

And in controller:在控制器中:

Route::group(['namespace' => 'Enchance\Sentrysetup\Controllers'], function()
{
    Route::get('slap', 'StatController@index');
});

In boot function:在启动功能中:

public function boot()
{
    require __DIR__.'/routes.php';     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM