404 Not Found, what is wrong on Route and Controller - php

I was tried to find the problem, but still not found what is wrong on my code.
Can anybody help what is wrong on my code.
route (web.php)
Route::get('/pegawai','PegawaiController#index');
PegawaiController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PegawaiController extends Controller
{
public function index()
{
//mengambil data dari table pegawai
$pegawai = DB::table('pegawai')->get();
// mengirim data pegawai ke view index
return view('index',['pegawai' =>$pegawai]);
}
}
My database is using mysql and for another program is can run just for this is 404 not Found.
I'm using laravel 8.6
And I tried to create new project and is no problem, but on existing project is always 404 Not Found
Any idea why this happen??

change route to :
Route::get('/pegawai', [PegawaiController::class, 'index']);
and check htaccess file exist in public folder
run php artisan cache:clear

if you are using the latest version of laravel, then you should use the path of laravel in route like ---
Route::get('/pegawai',[\App\Http\Controllers\PegawaiController,'index']);
if show some error please remove \ in the controller path.
Hope it works for you.

I was solved this case,
just code on command prompt php artisan route:cache and then my code is work

You should contain the namespace in Route::get facade function second parameter.
AS-IS
Route::get('/pegawai','PegawaiController#index');
TO-BE
Route::get('/pegawai','App\Http\Controllers\PegawaiController#index');
or
use App\Http\Controllers\PegawaiController;
Route::get('/pegawai', PegawaiController::class);

Related

Controllers not working on Laravel 8 despite uncommenting in RouteServiceProvider

I try to create routes to my index.blade.php page, i've made a controller "ProductController" using cmd php artisan make:controller ProductController, so in http --> Controllers i do have a ProductController.php file and i put this code in it :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductContoller extends Controller
{
public function index()
{
return view('products.index');
}
}
and then in my web.php i create a route using this code
Route::get('/boutique', 'ProductController#index');
However it doesn't work.
Firstly, when i go to the pretty url i've setup for my project at localhost using Laragon --> Projetname.test i get the normal Laravel Welcome page, but when i try to go to the url i've just setup like : ProjectName.test/boutique, i get
"Target class [App\Http\Controllers\ProductController] does not exist."
After reading about the changes since the update of Laravel to V8 here, i've seen that the update made some requirements for routes since $namespace prefix are not enabled automatically, but however that can be enabled again by uncommenting this line in RouteServiceProvider
// protected $namespace = 'App\\Http\\Controllers';
I do uncommenting that line and then clear the cache using php artisan route:cache, however it still not working..
When i first started doing research about routes issues in Laravel i've seen many forum spotted out that apache Allowoverride settings in httpd.config file may cause issue, so i change it settings from None to All and then restart Laragon but nothing works.
After correcting label on my controller it still do not work, i try both methode (the old and the new one) none of them works for me, cmd keeps returning me :
λ php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [ProductController] does not exist.
at C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:835
831▕
832▕ try {
833▕ $reflector = new ReflectionClass($concrete);
834▕ } catch (ReflectionException $e) {
➜ 835▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
836▕ }
837▕
838▕ // If the type is not instantiable, the developer is attempting to resolve
839▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:833
ReflectionException::("Class "ProductController" does not exist")
Laravel 8 Route should be
Route::get('/boutique', [NameController:class,'method']);
So in your web.php file add
use App\Http\Controllers\ProductContoller
Then write your route like this:
Route::get('/boutique', [ProductContoller::class,'index']);
And I think there is a missing 'r' in your "ProductController" class name
I found out after watching a tutorial here on most common issues with new routing methodes on Laravel 8 that when uncommenting the RouteServiceProvider, using the old method require to use the old route method too on web.php, so it looks like that :
Route::get('/boutique', 'ProductController#index');
Please use
Route::get('/boutique', '\App\Http\Controllers\ProductController#index');
or use name route group and indicate the namespace
Route::group(['namespace' => 'App\Http\Controllers'], function(){
Route::get('/boutique', 'ProductController#index');
});
let me know how it works.

I am unable to access Laravel view

I have just created one of about view in view/about.blade.php, and I am accessing this from localhost/myproject/public/about, but it's not working.
However, localhost/myprojects/public/ is working fine; about view has been created on same parameters as welcome by default in Laravel.
Firstly the information is not sufficient to say anything.Please provide your route.Also its important how you are running your project ,is it via Xampp(or Lampp whatever is there) or "php artisan serve"
but looking from your working directory "localhost/myprojects/public" I guess its not by the command . Try localhost/myprojects/public/about.blade.php or run it by php artisan serve and try route localhost:8000/about
Have you added particular routing to web.php file?
Route::get('about', function () {
return view('about');
});
https://laravel.com/docs/5.7/routing
Which error are you getting?
404 - Not found
Route::get('/about', function () {
return view('about');
});
Check routes
php artisan route:list
Laravel is a MVC Framework, Which means You Have a Controller which procede some logic when some request come in and interact with the model if need, after that the controller return some view.
And because you whan to acccess your view file, you must past through controller, and that controller will render the view. Because the views folder is not in the \public dicretory as subdirectory you can't access to It with url like localhost/myproject/public/about even if you get access to it, you will not get HTML, you'll get some plain text with Blade tags. It's a must to return view in you controller by rendering it, somewhere in the background Laravel procede all Blade Tag and return HTML that correspond to that tags.
What I can suggest you Is to create some route in your route file like this
Route::get('/about', function(Request $request){
// Automatically Laravel will look this file in the view directory
return view('about');
});
Or you can go with the controller like procedure by creating some controller, go in your terminal and execute
php artisan make:controller AboutController
this will generate a file name AboutController.php in app\Http\Controllers diretory within witch you will found
namespace App\Http\Controllers;
class HomeController extends Controller
{
}
after that add
public function index()
{
return View::make('about');
}
Don't forget to include use the Illuminale\Supports\Facades\View on top of your file
And one more important thing which left is to configure the Route, for that go in routes directory in the web.php file add
Route::get('/about', 'AboutController#index')->name('about');

Showing wrong view in laravel 5.4

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.

Laravel unable to load view

Just started learning laravel I tried to create a route for my view but when I load on the web browser it says Sorry, the page you are looking for could not be found. Can any one help me out in codeigniter it was so simple just create a controller and view and we can see on the web broswer I found laravel to be difficult then codigniter is that true?? can anyone define me how is the mvc structure for laravel5 as i found tutorials but they are of old laravel and files and structure are almost change so I got confused any suggestions please
routes.php
Route::get('main', 'Main#index');
Main.php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class main extends Contoller {
public function _construct() {
$this->middleware('guest');
}
public function index() {
return "Hello World from controller";
}
}
if you are running laravel project locally, it can run through its own server. you dont need apache server form wamp or xampp,,but you will need their mysql database. So start only that if you require database.
Now go to command prompt, navigate to the directory where your project is stored eg cd c:/wamp/www/yourprojet and then type following command
php artisan serve
it will start on port 8000 by default. and you can now access your project at 'http://localhost:8000/'
and you can access your view at 'http://localhost:8000/main'
Also you can find laravel tutorials and other at laracast
Try to change the class name to Main (now is main, in lowecase)
I learnt Laravel by their awesome tutorial: https://laravel.com/docs/4.2/quick
make a view in resources->views, something like my-view.blade.php
Then return view('my-view');
That my-view.blade.php can have whatever HTML you want in it
Go to your resources/views folder create file with filename.blade.php.
Now in your routes.php:
Route::get('main', 'Main#index');
And in your controller add this function:
public function index() {
return view('filename');
}

how to invoke a specific controller action in laravel

I am very new to Laravel. I created new Controller as
class ContactController extends Controller {
public function index(){
die("X");
}
}
And in the routes.php I wrote
Route::get('contact', 'ContactController#index');
I think hitting the following url the "X" must be printed. But it says "Not Found" error.
http://localhost/lapp/public/contact/index
What thing I am missing? Please guide me.
If the apache module_rewrite is not activated. Activate it. It will surely work.
Your guess is right. But the problem is with you URL.
it should be http://localhost/lapp/public/contact
Alternatively you can use php artisan serve command to start a server which would start the server on something like http://localhost:8080 Then you can access the URL like: http://localhost:8080/contact

Categories