gabahulk
gabahulk

Reputation: 611

Laravel 4 subdirectory controller doesn't load Input class

I'm new to Laravel and having trouble with subdirectories. I want to make an admin folder inside the controllers folder and so far it's working. but when I try to use Laravel's Input class it says that it couldn't find it.

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:

"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:

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

It still did not work.

Edit 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.

Answer:

After doing the modifications suggested by @ChristopherRathgeb and adding \ to the View and Input classes(example $input = \Input:all();) it worked! And now to redirect to these controller with the action method I just used action(admin\ProductsController) and it worked!

My thanks to all who helped!

Upvotes: 3

Views: 2992

Answers (2)

Christopher Rathgeb
Christopher Rathgeb

Reputation: 1687

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. If you are still using php 5.3 swap out the [] for array().

Upvotes: 6

c-griffin
c-griffin

Reputation: 3026

Import the Input class into the namespace you're using.

<?php namespace admin;

use \Illuminate\Support\Facades\Input;

    class ProductsController extends \BaseController {
.....

Or call Input from its namespace:

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

Upvotes: 1

Related Questions