Laravel NotFoundHttpException in RouteCollection.php line 161 - laravel

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');

Related

Laravel edit route for basic CRUD application giving 404

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

Route group prefix not working in Laravel 8

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'); });

gives a 404 response when using /admin on the url

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.

Can't access Dingo Api routes in localhost

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');

Laravel 5 add back routes.php NotFoundHttpException

In the last update to to Laravel 5 they removed the routes.php file in favor of Annotations. I still wish to use the routes.php file though. I read that you simple create the file in app/Http/routes.php and uncomment
//require app_path('Http/routes.php');
Inside the RouteServiceProvider.php file. I tried and I still get the NotFoundHttpException. For the following route.
Route::get('/', 'PagesController#home');
Inside the PagesController
<?php namespace App\Http\Controllers;
class PagesController {
public function home()
{
return 'test';
}
}
Can anyone tell me how to re enable it?
After uncommenting //require app_path('Http/routes.php'); you need to run:
php artisan clear-compiled
Otherwise your RouteServiceProvider is compiled and Laravel won't see the change you made in this file source

Resources