简体   繁体   English

Laravel 4子目录控制器不加载Input类

[英]Laravel 4 subdirectory controller doesn't load Input class

I'm new to Laravel and having trouble with subdirectories. 我是Laravel的新手,在子目录方面遇到了麻烦。 I want to make an admin folder inside the controllers folder and so far it's working. 我想在controllers文件夹中创建一个admin文件夹,到目前为止它正在运行。 but when I try to use Laravel's Input class it says that it couldn't find it. 但是当我尝试使用Laravel的Input类时,它说它无法找到它。

My routes: 我的路线:

Route::group(array('prefix'=> 'admin', 'before' => 'auth.admin'), function() {

   Route::resource('/users','Admin\\UsersController');
   Route::resource('/products','Admin\\ProductsController');
   Route::resource('/categories','Admin\\CategoriesController');
   Route::resource('/orders','Admin\\OrdersController');
   Route::resource('/reviews','Admin\\ReviewsController');

});      

The Products Controller: 产品控制器:

 <?php namespace admin;

    class ProductsController extends \BaseController {

    protected $layout = 'master';
        /**
     * Instantiate a new ProductsController instance.
     */
    public function __construct()
    {
        $this->beforeFilter('auth.admin');
    }

    /**
     * Display a listing of the resource.
     * GET /products
     *
     * @return Response
     */
    public function index()
    {
        $input = Input::all(); //here is where it finds the error

And the composer.json autoload: 而composer.json自动加载:

"autoload": {

        "classmap": [
            "app/commands",
            "app/controllers/",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php",
            "app/controllers/Admin"
        ]
    },

Thank you! 谢谢!

Edit: 编辑:

I have also tried to use Input (and \\Input) and it returned the "Class 'Facade' not found" error, and when I tried: 我也尝试使用Input(和\\ Input)并返回“Class'Facade'not found'错误,当我尝试时:

use \Illuminate\Support\Facades\Facade;
use Input;

It still did not work. 它仍然无法正常工作。

Edit 2: 编辑2:

Now using: 现在使用:

use Illuminate\Support\Facades\Input;

and returning the same error. 并返回相同的错误。

Edit 3: Did the modifications suggested by @ChristopherRathgeb and now it's not finding the products model. 编辑3: @ChristopherRathgeb建议修改,现在它没有找到产品型号。

Answer: 回答:

After doing the modifications suggested by @ChristopherRathgeb and adding \\ to the View and Input classes(example $input = \\Input:all(); ) it worked! 在完成@ChristopherRathgeb建议的修改并将\\添加到View和Input类(例如$input = \\Input:all(); )之后就可以了! And now to redirect to these controller with the action method I just used action(admin\\ProductsController) and it worked! 现在要使用动作方法重定向到这些控制器我刚刚使用了动作(admin \\ ProductsController)并且它工作了!

My thanks to all who helped! 感谢所有帮助过的人!

First off you can use a route group based on the namespace: 首先,您可以使用基于命名空间的路由组:

Route::group(['namespace'=>'admin','prefix'=> 'admin', 'before' => 'auth.admin'],function() {
   Route::resource('/users','UsersController');
   Route::resource('/products','ProductsController');
   Route::resource('/categories','CategoriesController');
   Route::resource('/orders','OrdersController');
   Route::resource('/reviews','ReviewsController');
});

Next your issue with input is that you need to include the Input facade: 接下来您的输入问题是您需要包含输入外观:

Remove this: 删除这个:

use \Illuminate\Support\Facades\Facade;
use Input;

and Add the following to the top of the file: 并将以下内容添加到文件的顶部:

use Illuminate\Support\Facades\Input;

NOTE: This answer uses php 5.4 array syntax. 注意:这个答案使用PHP 5.4数组语法。 If you are still using php 5.3 swap out the [] for array(). 如果您仍在使用php 5.3,请将[]替换为array()。

Import the Input class into the namespace you're using. Input类导入到您正在使用的命名空间中。

<?php namespace admin;

use \Illuminate\Support\Facades\Input;

    class ProductsController extends \BaseController {
.....

Or call Input from its namespace: 或者从其命名空间调用Input

public function index()
{
    $input = \Illuminate\Support\Facades\Input::all(); //here is where it finds the error

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

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