3

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!

7
  • It seems weird that you would need to use Input or Facade as they should be automatically registered as soon as the app is started. Did you composer dump-autoload after moving the controllers into the subdirectory? Commented Aug 19, 2014 at 19:04
  • Thank you for your reply @Don't Panic , and i dit run composer dump-autoload successfully, but it's still not working (and driving me crazy haha) Commented Aug 19, 2014 at 19:23
  • Try removing the `Admin\\` portion from the route. After doing composer dump-autoload, the autoloader should be able to get the controllers without needing to specify the subdirectory. Commented Aug 19, 2014 at 19:27
  • I did that and got a "Class ProductsController does not exist" error Commented Aug 19, 2014 at 19:31
  • If you look at the composer/autoload_classmap.php, does it have your controller files listed in the proper location? Commented Aug 19, 2014 at 19:34

2 Answers 2

6

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().

Sign up to request clarification or add additional context in comments.

18 Comments

Thank you for the answer! I have followed you suggestion but i still get the "Class 'Facade' not found" error. :/
Do you have any sample code that shows your usage of Facade? I do not know how you would be using it in your controller like this. Typically the Facade class is subclassed.
sorry but I don't think i have any sample code of me using Facade, i actually don't even know what it is... If you could be more especific about the file you would like to have i look i'd be glad to post it!
$input = Input::all(); is the usage of Facade, right? class Input extends Facade.
Important: Make sure the use statement comes after the namespace statement, or PHP will not recognize it properly.
|
1

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

6 Comments

Thank you for your answer! But unfortunately id did not work... it returned the error "Class 'Facade' not found" and when I tried to 'use Facade;' it still did not work...
that isn't the namespace for Facade. Try use \Illuminate\Support\Facades\Facade;
Now I have used use \Illuminate\Support\Facades\Facade; and it's still not working :/
Have you tried calling Input using the second option? $input = \Input::all(); without a use statement? That may be better suited for your needs in this case. Facade shouldn't be called directly, but included with the Input class.
@Christopher Rathgeb is right. I apologize. it is actually, use \Illuminate\Support\Facades\Input; Sorry!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.