404 NOT FOUND in Laravel 8 - php

I am creating a simple website in Laravel while running a web site ran into the problem with 404 NOT FOUND Laravel 8. index page working when I click on about us and contact us page getting error of 404 NOT FOUND I don't know why is that. what I tried so far I attached below.
Folder structure
Controller
class SmsController extends Controller
{
public function index()
{
return view('index');
}
public function aboutus()
{
return view('aboutus');
}
public function contactus()
{
return view('contactus');
}
}
Index.blade.php I made the links
<ul class="nav navbar-nav">
<li class="active">Home</li>
<li>Aboutus</li>
<li>Contactus</li>
</ul>
routes
Route::get('/', 'App\Http\Controllers\SmsController#Index');
Route::get('Aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('Contactus', 'App\Http\Controllers\SmsController#contactus');

I think you got error here
Route::get('Aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('Contactus', 'App\Http\Controllers\SmsController#contactus');
instead
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('/contactus', 'App\Http\Controllers\SmsController#contactus');

I use this command:
php artisan route:cache
then
php artisan serve
After that, copy the IP address after second command.

In laravel 8 you can try this way:
Route::get('/', [App\Http\Controllers\SmsController::class, 'index']);
Route::get('/about-us', [App\Http\Controllers\SmsController::class, 'aboutus']);
Route::get('/contact-us', [App\Http\Controllers\SmsController::class, 'contactus']);

I had to use php artisan route:cach command in terminal.

In my laravel 9 case, I added a new attribute style routes and it gave 404 error, from console writing
`php artisan route:clear`
has solved my issue.

try to write a route like this
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');

Thank you guys, I have been on this for close to two days, this is what helped me
php artisan route:cache
using v9

The problem is with the route address since the Laravel route are case sensitive.
the correct form of route
Route::get('/aboutus', 'App\Http\Controllers\SmsController#aboutus');
Route::get('/contactus', 'App\Http\Controllers\SmsController#contactus')'
and also it is worth mentioning that the updated accepted way to write routes in the Laravel 8.x to-class method is
Route::get('/aboutus', [App\Http\Controllers\SmsController::class, 'aboutus']);
Route::get('/contactus', [App\Http\Controllers\SmsController::class, 'contactus']);
but the old method is still accessible on the newer version of Laravel

Related

Laravel routes dont show

I am completely new to laravel, so I think my question is a bit ridiculous.
I tried to create a route in the routes/web.php
Route::get('/example', function () {
Return ('hello');
)};
But when I try to see the example page, I receive 404.
I use php artisan serve to load the local host.
I couldn't find a solution relatable to my situation. Still no luck.
Windows version 8.1
Laravel version 8
Also I can't edit
Route::get('/', function () {
Return view ('welcome');
});
The changes won't apply
Thank you
You can type php artisan route:list to see if the route saved or not, if not you can write :
php artisan optimize:clear
Your syntax is not correct.
Route::get('/example', function () {
Return view('hello');
});
Make your hello.blade.php file add your html code that is your view file.run command php artisan route:clear.
you must use:
php artisan:serve
then
your_app_url/example
and edit web.php -- you have syntax error in web.php
Route::get('/example', function () {
return 'hello';
});

Can i use Route::get('/') normally on Laravel?

I have a Laravel 8 project. Everything work fine except the code below
Route::get('/', function ()
{
return redirect('/login');
});
Those code only run when i clear the route cache with php artisan route:clear or it will return this error everytime after i cache route:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: HEAD.
I want my users can go to login page despite of they access the http://localhost/myproject/public/ or http://localhost/myproject/public/login
If anyone have the answer, pleas help me. Thank you!
enter image description here
1.Add this at the top of your web.php use Illuminate\Support\Facades\Route;
2.
Route::get('/greeting', function () {
return 'Hello World';
});
Routing Laravel

The GET method is not supported for this route.Supported methods: HEAD

I just installed Laravel 8 and created new controller and route. when i try to use new route that i created which is working fine but route('/') is not working. giving me error
The GET method is not supported for this route. Supported methods: HEAD.
route/web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('login', [LoginController::class,'loginShow'])->name('login');
LoginController
class LoginController extends Controller
{
function loginShow(){
return view('Login.login');
}
}
Route List
Problem Detail :
here i have 2 routes 1) mydomin or mydomin/ 2) myDomian/login
here myDomian/login is working as i want but when i try to use mydomin then i am getting
many time i face thing problem but sometime i fix anyway. But i want to know its real reason why it happen? so next time i will take case self. here mydomin is GET method route and i delcleared in web.php as GET. so why it is telling it is HEAD not get? Also note that before create a new route it was working in same way. so why now not? please tell me reason of this.
i got my solution using php artisan serve . so after that it is start working. but i a could not understand why it is not working without php artisan serve .... if any one has any reason please tell us.

Custom 404 laravel

If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG

404 in Controller Action Method : Laravel 5.2.37

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.

Categories