I am new to laravel. I am trying to organise my controller by putting it inside a folder, but it doesn't seem to work.
My folder structure is like this:
/app
/Http
/Controllers
/Admin
ShowDashboard.php
My ShowDashboard.php file is like this:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function init()
{
return 'Hi there!';
}
}
My route is like this
Route::get('/admin', 'Admin\ShowDashboard@init');
When I tred to access http://localhost:8000/admin I get the following error:
Class App\Http\Controllers\Admin\ShowDashboard does not exist
My autolaoder section:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
Am I missing something?
The best way to create a controller is to use the built in Laravel utility, Artisan. From a command prompt, browse to the directory your laravel project is located. For example: c:\development\htdocs\www.example.dev
At the prompt, type: php artisan make:controller admin/showDashboard --plain
This will generate a file named showDashboard.php within an admin directory under your controllers. The file will have the following code by default:
Now that you have created your controller, add a method for init:
Your controller will now look like this:
Now, setup your route in your routes.php as follows:
Save your work, and launch your page. When browsing to www.example.dev/admin you should see the message: Hi there!
I hope this helps!
Everything is already explained but one more try can be done by adding
controller
suffix toshowDashboard
and runcomposer dump-autoload
.I think then your controller will run.
Rename your controller ShowDashboardController
The following code is working.. Try once
created a file ShowDashboard.php in folder admin like app/http/controller
now , ShowDashboard.php
Added
Route::get('admin', 'admin\ShowDashboard@init');
in routes.phpand then run
composer update
on cmd.. Then runhttp://localhost:8000/admin
. it says.. Hi there!I don't know why this was happening, but adding this in my route fixed it.
I don't see anything wrong with what you posted. If you changed the namespace-to-folder mappings in composer.json, make sure you ran the 'composer dump-autoload' command.
File name should be
ShowDashboardController.php