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');
});
Related
LARAVEL
when I make a controller insid folder called "Admin" by command
"php artisan make:controller Admin\ControllerLanguages"
and Route the page This error appears:
Method App\Http\Controllers\LanguagesController::index does not exist.
Bad Method Call Did you mean
App\Http\Controllers\LanguagesController::validate() ?
but when i make the controller normally in the default folder by command:
"php artisan make:controller LanguagesController"
the route runs and the page appears, i want the page appears when i create it in "Admin" Folder, I tried many ways, but to no avail.
You should declare the namespace for the route group
Route::prefix('languages')
->namespace('App\Http\Controllers\Admin')
->group(function() {
Route::get('/', 'LanguagesController#index')->name('admin.languages');
//All other Routes for languages defined here
//LanguagesController is at app/Http/Controllers/Admin folder
});
Or you can import the namespace via use statement like, at the top
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\LanguagesController;
Route::prefix('languages')
->group(function(){
Route::get('/', [LanguagesController::class, 'index'])->name('admin.languages');
//Other languages routes here
});
I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController#login');
Route::any('register', 'Api\v1\AuthController#register');
});
and creating AuthController in app/http/controller/Api/v1 directory:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404 error on this link:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?
Routes in api.php are automatically prefixed with /api. Currently, your routes are:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login is a 404.
If you remove /Api, and just use Route::prefix('/v1') ... then you should have no issue.
Also, always double check your routes with php artisan route:list to see what's wrong.
The API Routes are already prefixed by /api . I think the correct structure you'd looking for would be
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController#login');
Route::any('register', 'AuthController#register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route
http://127.0.0.1:8000/api/v1/login
You can use many ways to define routes for API in laraval > routes > api.php file.
In this i'm going to explain how we can use routes group in the laraval..
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods
php artisan make:controller Customers/CustomerController --resource
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.
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'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');