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.
Related
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);
I face a problem while trying to create a router under namespace "Instructor" as per the image below:
Route in instructor page in routes file
and the method under the namespace Instructor in the controller is:
method inside the controller file
it keeps giving me the following error when running the route:
Error message
can anyone help me solve this issue as I am new to larevel so I am not a pro in defining the packages and dependencies.
In Laravel 8, you can simply do the following in your route file:
Route::get('instructors', [\App\Http\Controllers\Instructor\FirstController::class, 'showUserName']);
Or with quotes:
Route::get('instructors', ['\App\Http\Controllers\Instructor\FirstController', 'showUserName']);
Or with use statement:
use App\Http\Controllers\Instructor\FirstController;
Route::get('instructors', [FirstController::class, 'showUserName']);
You can then have all your use statements at the top. Most IDE can automatically hide them so you have a clean route file.
In Laravel 8 you can do:
Route::get('your-route', [App\Http\Controllers\Instructor\YourController::class, 'methodName']);
Or you can uncomment following line in RouteServiceProvider
// protected $namespace = 'App\\Http\\Controllers';
I am trying to use the slim framework to build a website, however I get the following error: "Cannot call index on Store\HomeController because it is not a class nor a valid container entry." I have thoroughly checked for spelling and punctuation errors and found none. The error go's to line 98 of a "callableResover.php file of the following code:
throw new NotCallableException(sprintf(
'Cannot call %s on %s because it is not a class nor a valid container entry',
$callable[1],
$callable[0]
));
This is the HomeController.php file the I created as the following:
namespace Store\Controllers;
class HomeController{
public function index(){
echo('Index');
}//end function index
}//end class
And this the route.php file of the code following:
$app->get('/', ['Store\HomeController', 'index'])->setName('home');
Assuming your HomeController file is loaded before using it in your route.php file, try to:
1. Add a \ to your controller's namespace in the route definition
\Store\HomeController
2. Change your route to this one instead
$app->get('/', \Store\HomeController::class . ':index');
Check the Slim documentation to learn more about container resolution
I found a problem..
The class is probably registred in /vendor/composer/autoload_classmap.php
so you have to regenerate it with command
composer dump-autoload
and it should work...
I have controllers in different folder than Laravel native App\Http\Controllers. I am using a custom Lib\MyApp folder which has modules inside. Each module has its own controllers, models etc. I added to composer.json autoloading to app\lib.
What I did is change RouteServiceProvider namespace:
protected $namespace = 'App\Lib\MyApp';
I did a composer dump-autoload after everything.
Inside MyApp is a Landing\Controller folder with actual controller class inside.
Try 1 (ideal):
I would like to call my route like this:
Route::get('/', 'Landing\Controller\LandingController#index');
But this way I am getting a ReflectionException that the class is not found even though
Try 2:
Route::get('/', '\Landing\Controller\LandingController#index');
Trailing slash gets rid of the namespace part when I refresh the page, and class is still said not to exist.
Try 3:
Route::get('/', 'MyApp\Landing\Controller\LandingController#index');
This just duplicates MyApp folder, and class is not found as expected.
Try 4 (working, but don't want it like that)
Route::get('/', '\MyApp\Landing\Controller\LandingController#index');
This works fine, although I would like to get rid of the \MyApp\ part.
Is something like this possible?
You can use the namespace in the routes for that purpose :
Route::namespace('Landing\Controller')->group(function () {
Route::get('/', 'LandingController#index');
// + other routes in the same namespace
});
And dont forget to add the namespace to the controllers :
<?php namespace App\Lib\MyApp\Landing\Controller;
PS : in the case where the Lib is inside the App folder there is no need to add a thing in the composer file, because the App folder is registred in the psr-4 and with this it will load all the files within this namespase for you.
There are many ways to add the namespace in Laravel
Route::group(['prefix' => 'prefix','namespace'=>'Admin'], function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
Route::namespace('Admin')->group(function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
//single route
Route::namespace('Admin')->get('/todo', 'TaskController#index');
//single route
Route::get('/todo', 'Admin/TaskController#index');
// by ->namespace
Route::prefix('admin')->namespace('Admin')->group(function () {
// route code
});
For "laravel 8"
Here I have given an example with both namespace and prefix but you can also use any one according to your requirement.
I created Controller in Controllers dir with command
php artisan make:controller Admin/StoriesController
Route::namespace('Admin')->prefix('admin')->group(function(){
Route::get('/deleted_stories',
'\App\Http\Controllers\Admin\StoriesController#index')->name
('admin.stories.index');
});
I have an issue with the admin part of my website using CakePHP 3.2.
This part works really well on wamp in local but when I moved the site to the apache server, it stopped working. I have this error message :
Missing Controller Cake\Routing\Exception\MissingControllerException
Error: DashboardController could not be found. Error: Create the class DashboardController below in file: src/Controller/Admin/DashboardController.php
And this error in the variables :
error : Unserializable object - Cake\Routing\Exception\MissingControllerException. Error: Controller class Dashboard could not be found in /data/vhosts/dev.droplet.ninja/htdev/vendor/cakephp/cakephp/src/Routing/Dispatcher.php, line 79
But the Controller exists at the right path with this content :
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class DashboardController extends AppController
{
public function index()
{
}
}
The prefix in my routes.php is :
// Admin namespace
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Dashboard', 'action' => 'index', 'dashboard']);
$routes->fallbacks('DashedRoute');
});
The routes works fine for the public part of the website but not for this. It seems that it can read the prefix and try to go to the file and even ask me to create the exact same file I already have. The only mistery is why it can't find him.
Also the Controller name is in :
src/Controller/Admin/DashboardController.php
I was looking for the differences between the two apaches settings without finding what can make cakePhp have this behavior.
Do you have any idea ?
Thank you
There's a multitude of reasons why it may not work. In my case, it was because of the old routes cache which I had to clear.
bin/cake cache clear _cake_routes_
You can get the list of cache prefixes by running bin/cake cache list_prefixes.
More info: /3.0/en/console-and-shells/cache.html