Class App\Http\Controllers\HomeController does not exist
HomeController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class HomeController extends Controller
{
public function index()
{
$posts = Post::paginate(10);
return view ('pages.index', ['posts' => $posts]);
}
public function show($slug)
{
$post = Post::where('slug', $slug)->firstOrFail();
return view ('pages.show', compact('post'));
}
}
web.php
Route::get('/', 'HomeController#index');
Route::get('/post/{slug}', 'HomeController#show')->name('post.show');
Route::group(['prefix'=>'admin','namespace'=>'Admin'], function(){
Route::get('/', 'DashboardController#index');
Route::resource('/categories', 'CategoriesController');
Route::resource('/tags', 'TagsController');
Route::resource('/users', 'UsersController');
Route::resource('/posts', 'PostsController');
});
At the beginning a new authorization controller appeared, I turned off the KG and removed
Five most important commands if your Laravel is not working as expected after some modifications in .env or database folder or because of any other modifications. Here is full explanation: https://www.youtube.com/watch?v=Q1ynDMC8UGg
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear
Please have a look at this thread: https://stackoverflow.com/a/43041479/6935763
Run this command to clear all compiled files:
php artisan clear-compiled
See more as this commands here:
https://laravel.com/docs/5.8/artisan
Hope this helps!
In some cases adding the controller directory resolves this issue. Please check the controller directory and make changes according to that.
Normally it is App\Http\Controllers
if so, then you can try with changing the route code to the following:
Route::get('/', 'App\Http\Controllers\HomeController#index');
Route::get('/post/{slug}', 'App\Http\Controllers\HomeController#show')->name('post.show');
check your controller directory.
Related
I have created a Middleware via php artisan make: middleware AdminCheck
Registered it in kernal.php
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\AdminCheck::class,
'auth' => \App\Http\Middleware\Authenticate::class,
//default middlewares.....
];
In web.php this is how I'm using it for all my routes
Route::prefix('/app')->middleware('admin')->group(function () {
Route::post('/create_tag', 'AdminController#addTag');
Route::get('/get_tags', 'AdminController#getTag');
Route::post('/edit_tag', 'AdminController#editTag');
});
Here is my The Middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
class AdminCheck
{
public function handle(Request $request, Closure $next)
{
Log::info("Middleware Reached");
dd("Middleware Reached");
if (!Auth::check()) {
//l redirect to login
return redirect('/login');
}
return $next($request);
}
}
The middleware is working since the routes are still accessed even without login and there's nothing in the log and its not die dumping either
For similar issues in the future, kindly check your cache, you can do so by doing
php artisan about
To clear your cache you can do:
php artisan cache:clear
php artisan view:clear
php artisan config:clear
php artisan route:clear
It will list what is cached, if you are going to be making any changes to route/config/views you should clear the cache and cache again.
I'm working on Laravel 5.7 project and I'm trying to create a controller with this name AdminController with this command "php artisan make:controller foldername\AdminController" then I give it a route like this Route::get('/admin','AdminController#login');
the thing is I can't find the AdminController in my project folders? I searched in App/Http/Controller also it's not there?
You put your controller in a folder so you must call this folder name in Route and in namespace of controller.
Try "php artisan make:controller foldername/AdminController"
Your route must be:
Route::get('/admin','foldername\AdminController#login')
And your controller namespace is namespace App/Http/Controller/foldername
For example, by running the command php artisan make:controller PostController it will create the
PostController.php in app/Htttp/Controllers
And you can access in route:
Route::get('/posts', 'PostController#index')->name('posts.index');
Route::get('/posts/create', 'PostController#create')->name('posts.create');
Route::post('/posts', 'PostController#store')->name('posts.store');
Route::get('/posts/{post}', 'PostController#show')->name('posts.show');
Route::get('/posts/{post}/edit', 'PostController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostController#update')->name('posts.update');
Route::delete('/posts/{post}', 'PostController#destroy')->name('posts.destroy');
But in your situation, you are using the custom namespace. For example:
php artisan make:controller Admin\PostController
It will create the new folder inside the Controllers with file:
app/Http/Controller/Admin/PostController.php
Now you can't access for the route like the previous:
Route::get('/posts', 'PostController#index')->name('posts.index');
Or
Route::resource('/posts', 'PostController');
If you are using custom nameSpaces for the large number of controller, try below method:
Route::group(['namespace' => 'Admin'], function () {
Route::resource('/posts', 'PostController');
});
Or:
Route::group(['namespace' => 'Admin'], function ()
{
Route::get('/posts', 'PostController#index')->name('posts.index');
Route::get('/posts/create', 'PostController#create')->name('posts.create');
Route::post('/posts', 'PostController#store')->name('posts.store');
Route::get('/posts/{post}', 'PostController#show')->name('posts.show');
Route::get('/posts/{post}/edit', 'PostController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostController#update')->name('posts.update');
Route::delete('/posts/{post}', 'PostController#destroy')->name('posts.destroy');
});
I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.
First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})
Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers
There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.
I have following Controller Action Method.
namespace App\Http\Controllers\API\SportsType;
class SportsTypeApiController extends \App\Http\Controllers\Controller
{
public function apiSportsTypes() {
return 1;
}
}
Here is the route
Route::group(['prefix' => 'api/v1'], function () {
Route::get('/apiSportsTypes', 'API\SportsType\SportsTypeApiController#apiSportsTypes');
});
It gives 404 error. Am I missing something? Please let me know if you need more details.
It was happening because the routes were using cache. Due to that new routes were not even appearing in command prompt even if I was typing below command.
php artisan route:list
Then I had to remove the route cache by using the below command
php artisan route:clear
and now everything is working fine.
I'm new in Laravel and I can't understand why the controller doesn't work. Can you help me? Thanks.
This is the routes.php file:
routes.php
This is the WelcomeController controller:
WelcomeController.php
and this is the exception trowed:
ReflectionException in Container.php line 737:
Class App\Http\Controllers\WelcomeController does not exist
If you are using the laravel 8 then you can use the below-mentioned code in your web.php route file
use App\Http\Controllers\WelcomeController;
Route::get('/', [WelcomeController::class, 'index'])->name('welcome');
For more information use the laravel 8 documentation https://laravel.com/docs/8.x/routing
You need a WelcomeController.php file in directory Controllers, and you have it in Controllers/Auth
I'd suggest to:
$>php artisan make:controller WelcomeController
if you forget to choose your method:
Route::get('/', 'WelcomeController')->name('welcome');
change it to:
Route::get('/', [WelcomeController::class, 'index'])->name('welcome');
or
Route::get('/', 'WelcomeController#YOUR_METHOD_NAME')->name('welcome');