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');
Related
Hi everyone I need a solution with laravel project when I try open backend admin with /control its giving me an error "Not Found - The requested resource /control was not found on this server."
When I change the name "/control" to anything like "/control5" or something its working fine but the problem is I use /control at views and other! I am new to laravel I didn't know what the problem was? please help me out with this!
Web.php
Auth::routes();
Route::get('/about', [App\Http\Controllers\AboutController::class,'about']);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/', [App\Http\Controllers\HomePageController::class,'index']);
Route::get('/listing', [App\Http\Controllers\ListingPageController::class,'index']);
Route::get('/details', [App\Http\Controllers\DetailsPageController::class,'index']);
Route::group(['prefix' => 'control','middleware' => 'auth'],function(){
Route::get('/', [App\Http\Controllers\Control\DashboardController::class,'index'])->name('control');
//Pages
Route::get('/pages', [App\Http\Controllers\Control\PagesController::class,'index']);
Route::get('/pages/add', [App\Http\Controllers\Control\PagesController::class,'create']);
Route::get('/pages/edit', [App\Http\Controllers\Control\PagesController::class,'edit']);
});
DashboardController
namespace App\Http\Controllers\Control;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(){
return view('control.dashboard');
}
}
Because, you have a folder named control on /public folder. That error occurs when you create a folder in the public folder with the same name as your route so please change the name of the folder you have put in the public folder so that it has a different name from your route this will probably solve your error
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 created a multi auth in Laravel 5.3,
Then moved Controller/Auth/[files] to:
Admin: Controller/Admin/Auth/[files] &
Site: Controller/Site/Auth/[files]
In command line I type php artisan route:list,
It shows me following error:
Class App\Http\Controllers\Auth\LoginController does not exist
Where is my problem?
You need to manually define all the Auth routes in web.php and remove Auth::routes().
just define all your routes like,
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::get('/', 'Auth\LoginController#showLoginForm');
Route::post('login', 'Auth\LoginController#login');
Route::post('logout', 'Auth\LoginController#logout');
});
The two default authentication controllers provided with the framework have been split into four smaller controllers. The easiest way to upgrade your application to the new authentication controllers is to grab a fresh copy of each controller from GitHub and place them into your application.
https://github.com/laravel/laravel/tree/5.3/app/Http/Controllers/Auth
You should also make sure that you are calling the Auth::routes() method in your routes/web.php file. This method will register the proper routes for the new authentication controllers.
Pasted this answer from the Laravel upgrade documentation.
If you're moving controllers to a custom directory, you shouldn't use auth routes. So remove this from routes file:
If you're using 5.2
Route::auth();
If you're using 5.3
Auth::routes();
And then build auth routes manually.
In my case, I had the same issue when I type: php artisan route:list
As I am using Laravel 8.x, I noticed there is a tiny change in the notations which I had to apply in routes/web.php in 2 steps:
use App\Http\Controllers\LoginController; // step 1
Route::post('/login', [LoginController::class, 'login']); // step 2
Hello please check your route/web.php
/* For get login page*/
Route::get('/login', function () {return view('auth.login');});
/* while post remember to user Auth\controllername so you can get the perfect path for the custom login */
Route::post('/login', 'Auth\LoginController#authentication')->name('login');
It Happens With Me Now And I Had Solved It By Another Way
Simply Copy The Auth Folder
And Put It In The Path Of Admin Controllers Folder
And Open Each File And Change
namespace App\Http\Controllers\Auth;
To
namespace App\Http\Controllers\Dashboard\Auth;
Hope It Just Helps Somebody
for me just i'am add Auth::routes(); in my routes file routes/web.php or any other routes file like routes/admin.php if you create it
add this
namespace App\Http\Controllers;
Download or copy RegisterController.php from another project.
Paste it in your project under
Controllers/Auth/[files]
That's it
I cloned this todstoychev/Laravel5Starter from Github and installed it.
After creating this StaticPagesController controller and updating my routes.php file. The controller does not seem to work. For some reason i keep getting the following error.
ReflectionException in ControllerInspector.php line 32:
Class App\Http\Controllers\StaticPagesController#faq does not exist
My routes.php file
<?php
// Admin routes
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::controller('permissions', 'AdminPermissionsController');
Route::controller('settings', 'AdminSettingsController');
Route::controller('roles', 'AdminRolesController');
Route::controller('users', 'AdminUsersController');
Route::controller('/', 'AdminController');
});
// Public and user routes
Route::controller('contacts', 'ContactsController');
Route::controller('users', 'UsersController');
Route::controller('/', 'IndexController');
Route::controller('faq', 'StaticPagesController#faq');
My StaticPagesController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StaticPagesController extends Controller
{
public function faq(){
return 'this is faq page';
}
}
I have tried composer update, php artisan acl:update, composer dumpautoload to no avail.
Please help me. Thanks
With this line:
Route::controller('faq', 'StaticPagesController#faq');
You are telling Laravel that the controller for faq shoule be StaticPagesController#faq. The Route::controller method sets an entire controller for a route, it does not specify a method to be used on that route, Laravel handles this internally. Take a look at your error to prove my point:
Class App\Http\Controllers\StaticPagesController#faq does not exist
It is looking for class StaticPagesController#faq not StaticPagesController as you are intending.
Unless you are building an API using REST, you should not use the controller method and instead specify your routes explicitly, i.e.
Route::get('faq', 'StaticPagesController#faq');
This will use the faq method on your controller when the user makes a GET request to the URI faq. If you insist on using the controller method, then remove the #faq from the second argument and you will be good, although I'm pretty sure Laravel expects the methods index, show, create, etc to be in your controller. I suggest taking a look at the Laravel 5 Fundamentals video course to help you get a better understanding.