I have a problem when using /admin on the url, if I use it always gives a 404 response but, when I change /admin on web.php in other words like adnnin etc. there was no problem
here piece web.php
Route::middleware('isadmin')->group(function () {
Route::prefix('admin')->group(function () {
Route::get('/dashboard', [App\Http\Controllers\Admin\DashboardController::class, 'index']);
});
});
here my controller
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index () {
return view('pages.admin.dashboard');
}
}
Make sure you do not have a folder named admin in your public directory. The webserver will look for folder/files before falling back to handing the request off to the front loader public/index.php, the Laravel application.
Related
I'm trying to set up a basic Laravel 9 CRUD application, and I cannot get the edit route working for the User Controller.
routes/web.php
Route::get('/dashboard', function () { return view('dashboard'); })
->middleware(['auth'])->name('dashboard');
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{$id}/edit','edit')->middleware('auth');
Route::post('user/{$id}/edit','update')->middleware('auth');
Route::get('user/{$id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
require __DIR__.'/auth.php';
UserController.php
class UserController extends Controller
{
function edit(int $id)
{
echo 123;
}
I'm getting a 404 NOT FOUND page
Also, why don't I see the stack trace for this error?
Also, in some examples, I've seen people using the model class name as the parameter type in the controller method declaration, such as:
function edit(User $id)
{
echo 123;
}
However, I've also seen other examples using int instead. So which is the correct one?
First, inside your .env filte you should put
APP_ENV=local
APP_DEBUG=true
and change your web.php to:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth'])->name('dashboard');
use App\Http\Controllers\UserController;
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{id}/edit','edit')->middleware('auth');
Route::post('user/{id}/edit','update')->middleware('auth');
Route::get('user/{id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
require __DIR__.'/auth.php';
Then, try to run
php artisan route:list
and check if your routes are correct.
And try to remove middlewares inside user files, maybe you do not have login page and it redirects you there.
Be sure you are in the correct url like localhost/user/1/edit
Parameters in routes don't take a starting $. Change all occurrences of {$id} in your routes to just {id}:
Route::controller(UserController::class)->group(function(){
Route::get('user','index')->middleware('auth')->name('cases');
Route::get('user/create','create')->middleware('auth');
Route::post('user/create','store')->middleware('auth');
Route::get('user/{id}/edit','edit')->middleware('auth');
Route::post('user/{id}/edit','update')->middleware('auth');
Route::get('user/{id}/delete','destroy')->middleware('auth');
Route::get('user/{id}','show')->middleware('auth');
});
More on Route Parameters
Edit: you might also want to take a look at Resource Controllers. Something like Route::resource('users', UserController::class); will manage all of the required routes
I was trying to make a group routing with a prefix in Laravel 8. But when I tested it in http://localhost/mysite/admin/test/, it always throws error 404.
Here is the code in web.php:
Route::prefix('/admin', function() {
Route::get('/test', [Admin\LoginController::class, 'index']);
});
I created a controller in app/Http/Controller/Admin/ as the controller is inside Admin folder.
Here is the code in LoginController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function __construct()
{
//
}
public function index()
{
echo "Please login";
}
}
Can anybody show me what I am doing wrong to get it working?
You have to group the routes as stated in the documentation like:
Route::prefix('admin')->group(function () {
Route::get('/test', function () {
// Matches The "/admin/users" URL
});
});
In your case it would be:
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('/test', [LoginController::class, 'index']);
});
I think it should be 'admin' not '/admin'.
That slash makes it:
http://localhost/mysite//admin/test
=>
http://localhost/admin/test
You can check all your routes using: php artisan route:list
Try this
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('test', ['LoginController::class, index'])->name('test'); });
Laravel 6.x.
I'm creating custom multi-authentication Panels(Staff,Student,Admin) from Single Login Page.Error already mentioned in the title. also without using php artisan ui bootstrap --auth.
Web.php file.
Route::get('/index/sign-in', function () {
return view('log-in');
});
Route::get('/index/admin', function () {
return view('admin-dashboard');
});
Route::get('/index/student', function () {
return view('student-dashboard');
});
Route::get('/index/staff', function () {
return view('faculty-dashboard');
});
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/','LoginController#postlogin')->name('postlogin');
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
});
LoginController.php file
public function postlogout()
{
auth()->logout();
//session()->flash('message', 'Some goodbye message');
return redirect('/index/sign-in/');
}
public function postlogin()
{
$role=(Auth::user())->user_role;
if ($role=='admin'){
return 'index/admin';
}
elseif ($role=='staff'){
return 'index/staff';
}
elseif ($role=='student'){
return 'index/student';
}else
return 'index/sign-in';
}
}
If you didn't move the controller from his default location, is inside the Auth folder, in the controller folder (app/Http/Controllers/Auth)
So as your error Target class [App\Http\Controllers\LoginController] does not exist it's searching for the controller in the Controllers folders but not in the Auth subfolder, so The route is wrong.
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
should be
Route::post('/index/logout','Auth\LoginController#postlogout')->name('postlogout');
Best Regards.
I'm not sure if this is the default in Laravel 6, but the LoginController is likely under the Auth folder/namespace`:
app
- Http
-- Controllers
--- Auth
---- LoginController.php
...
In this case, you need to reference the namespace in your routes:
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/', 'Auth\LoginController#postlogin')->name('postlogin');
Route::post('/index/logout', 'Auth\LoginController#postlogout')->name('postlogout');
});
In your web.php file check all namespace passed, if their directory root is passed correctly or not.
in the upper part of web.php file you need to make sure of each controller root.
Such as you are using LoginController so you must pass use App\Http\Controllers\LoginController; or use App\Http\Controllers\Controller\LoginController;
according to your Http\Controller project structure
If you did not create the controller with the artisan command, delete it and create it with the php artisan create:controller LoginController command. This should get the problem resolved.
Can you check the namespace of the controller
namespace App\Http\Controllers\Auth;
class LoginController extends Controller
All the above solution didn't work for me, what did was using
php artisan route:clear
I have my laravel project created inside laragon/www folder and I'm using dingo/api package for routing.
Here are my files
routes/web.php
<?php
Route::get('/', function () {
return view('welcome');
});
routes/api.php
<?php
use Dingo\Api\Routing\Router;
$api->get('test', 'TestController#index');
app/Http/Controllers/Api/TestController.php
<?php
namespace App\Http\Controllers\Api;
class TestController extends BaseController {
public function index() {
return 'test';
}
}
When I run artisan api/routes it gives me the URI api/test but when I try to reach it in my browser under http://localhost/myproject/public/api/test it returns error "Sorry, the page you are looking for could not be found" knowing that http://localhost/myproject/public/ returns the good result
try
<?php
$api = app('Dingo\Api\Routing\Router');
$api->get('test', 'TestController#index');
I began to study Laravel, I have this problem.
File: project/app/Http/routes.php
<?php
Route::get('about', 'PagesController#about');
In the directory project I do by terminal
php artisan make:controller PagesController
File: project/app/Http/Controllers/PagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
public function about()
{
return("About");
}
}
You need to enter in apache configuration file and specify AllowOverride Yes on the directory where laravel still working
You are accessing your / route in your provided image. Which is not defined in your routes.php file that's why you are getting this error.
Add
Route::get('/', function() {
return "hello world";
});
and then try this route: 192.168.1.101/laravel-p/public/
Note you've explained the about route in your above code. So hit this route
`192.168.1.101/laravel-p/public/about`
You are accessing your / route in your provided image. Which is not
defined in your routes.php file that's why you are getting this error.
Add
Route::get('/', function() {
return "hello world"; });
and then try this route: 192.168.1.101/laravel-p/public/
Note you've explained the about route in your above code. So hit this
route
192.168.1.101/laravel-p/public/about
Nothing..
enter image description here
<?php
Route::get('/', function() {
return "hello world";
});
Route::get('about', 'PagesController#about');