在Laravel 4中访问包控制器 [英] Accessing package controllers in Laravel 4

查看:73
本文介绍了在Laravel 4中访问包控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照《 Laravel 4文档.创建软件包后,我创建了一个"controllers"文件夹和一个routes文件.新的文件结构为:

I have created a package following the "Creating a Package" instructions in the Laravel 4 documentation. After creating the package I have created a "controllers" folder and a routes file. The new file structure is:

/src
    /Vendor
        /Package
            PackageServiceProvider.php
    /config
    /controllers
    /lang
    /migrations
    /views
    routes.php
/tests
/public

我将路由文件添加到了包服务提供商的启动部分:

I added the routes file to the boot portion of the package service provider:

public function boot()
{
    $this->package('vendor/package');
    include __DIR__ . '/../../routes.php';
}

然后将基本路由添加到路由文件:

Then added a basic route to the routes file:

Route::get('/package', function() {
    return "Package route test";
});

以指定的路径(app.dev/package)访问我的应用程序将返回预期的结果:

Visiting my application at the specified route (app.dev/package) returns the expected:

Package route test

然后向路由添加基本的控制器调用(使用默认的Laravel控制器"HomeController")即可:

Then adding a basic controller call to the route (using the default Laravel controller, "HomeController") works:

Route::get('/package', 'HomeController@showWelcome');

然后,我按照此SO答案设置了程序包的控制器.我将src/controllers文件夹添加到了作曲家的类映射中,然后我转储了自动加载器并检查了vendor/composer/autoload_classmap.php,发现该类已被作曲家成功加载:

I then followed this SO answer for setting up the controller for the package. I added the src/controllers folder to the composer classmap, then I dumped the autoloader and checked vendor/composer/autoload_classmap.php and found the class is successfully loaded by composer:

<?php

// autoload_classmap.php generated by Composer

$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);

return array(
    'HomeController' => $baseDir . '/src/controllers/HomeController.php',
);

现在,我使用名称空间将新的包控制器添加到路由:

Now I added the new package controller to the route using the namespace:

Route::get('/package', 'Vendor\Package\Controllers\HomeController@showWelcome');

但这会产生关于找不到类的错误:

but this produces an error about not finding the class:

ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist

我也尝试过使用包名称来调用它:

I've also tried calling it using the package name:

Route::get('/package', 'Package::HomeController@showWelcome');

会产生相同的错误:

ReflectionException: Class Vendor\Package\Controllers\HomeController does not exist

无论我尝试哪种方式,程序包都无法访问其自己的控制器,这是由作曲家确认已加载的(通过查看vendor/package/autoload_classmap.php).

No matter what I try the package cannot access its own controller, which composer confirms is loaded (by viewing vendor/package/autoload_classmap.php).

有什么想法吗?我不确定问题是否是作曲家未加载该类,也不确定从哪里开始调试问题.我创建了另一个程序包,并在这里重复了步骤,并得到了同样的问题.

Any ideas? I'm not sure if the issue is composer not loading the class, I'm not sure where to start with debugging the problem. I've created another package and repeated the steps here and get the same problem.

我可以从程序包和应用程序中访问程序包视图,例如:

I can access the package views from both the package and the app, eg:

View::make('package::view');

问题似乎出在作曲家加载控制器和Laravel能够访问它之间.

The problem seems to be between composer loading the controller and Laravel being able to access it.

推荐答案

错误是在路由中包括了控制器路径.我有以下内容:

The mistake was including the controllers path in the route. I had the following:

Route::get('/package', 'Vendor\Package\Controllers\HomeController@showWelcome');

正确的用法是:

Route::get('/package', 'Vendor\Package\HomeController@showWelcome');

控制器中包含命名空间:

With the namespace included in the controller:

namespace Vendor\Package;

控制器应扩展照明:

\Illuminate\Routing\Controllers\Controller

仍然不能使用包名称(例如:Package :: HomeController @ showWelcome),但是我可以使用名称空间.耶.

Still can't use the package name (eg: Package::HomeController@showWelcome), but I can using the namespace. yay.

问题解决了.

这篇关于在Laravel 4中访问包控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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