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

在 Laravel 4 中访问包控制器

Accessing package controllers in Laravel 4(在 Laravel 4 中访问包控制器)

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

问题描述

我已经按照 "Creating a Package" 中的说明创建了一个包Laravel 4 文档.创建包后,我创建了一个控制器"文件夹和一个路由文件.新的文件结构是:

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');

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

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', 'VendorPackageControllersHomeController@showWelcome');

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

but this produces an error about not finding the class:

ReflectionException: Class VendorPackageControllersHomeController does not exist

我也试过用包名来调用它:

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

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

产生同样的错误:

ReflectionException: Class VendorPackageControllersHomeController 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');

问题似乎在于 composer 加载控制器和 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', 'VendorPackageControllersHomeController@showWelcome');

正确的用法是:

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

在控制器中包含命名空间:

With the namespace included in the controller:

namespace VendorPackage;

控制器应该扩展照明:

IlluminateRoutingControllersController

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

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

问题解决了.

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

本文标题为:在 Laravel 4 中访问包控制器

基础教程推荐