Missing Controller when using a prefix admin in CakePHP 3.2 - php

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

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.

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

ReflectionException Class App\Http\Controllers\StaticPagesController#faq does not exist Laravel-5

I cloned this todstoychev/Laravel5Starter from Github and installed it.
After creating this StaticPagesController controller and updating my routes.php file. The controller does not seem to work. For some reason i keep getting the following error.
ReflectionException in ControllerInspector.php line 32:
Class App\Http\Controllers\StaticPagesController#faq does not exist
My routes.php file
<?php
// Admin routes
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::controller('permissions', 'AdminPermissionsController');
Route::controller('settings', 'AdminSettingsController');
Route::controller('roles', 'AdminRolesController');
Route::controller('users', 'AdminUsersController');
Route::controller('/', 'AdminController');
});
// Public and user routes
Route::controller('contacts', 'ContactsController');
Route::controller('users', 'UsersController');
Route::controller('/', 'IndexController');
Route::controller('faq', 'StaticPagesController#faq');
My StaticPagesController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StaticPagesController extends Controller
{
public function faq(){
return 'this is faq page';
}
}
I have tried composer update, php artisan acl:update, composer dumpautoload to no avail.
Please help me. Thanks
With this line:
Route::controller('faq', 'StaticPagesController#faq');
You are telling Laravel that the controller for faq shoule be StaticPagesController#faq. The Route::controller method sets an entire controller for a route, it does not specify a method to be used on that route, Laravel handles this internally. Take a look at your error to prove my point:
Class App\Http\Controllers\StaticPagesController#faq does not exist
It is looking for class StaticPagesController#faq not StaticPagesController as you are intending.
Unless you are building an API using REST, you should not use the controller method and instead specify your routes explicitly, i.e.
Route::get('faq', 'StaticPagesController#faq');
This will use the faq method on your controller when the user makes a GET request to the URI faq. If you insist on using the controller method, then remove the #faq from the second argument and you will be good, although I'm pretty sure Laravel expects the methods index, show, create, etc to be in your controller. I suggest taking a look at the Laravel 5 Fundamentals video course to help you get a better understanding.

Unable to change admin prefix in cakephp 1.3

I have built admin of my cakephp site using prefix 'webadmin'. Now I need to change this to something like 'aRRT6nnf'.
I changed the admin prefix in core.php, routes.php and even changes the filenames in views folder but this gives the following error:
Error: The requested address '/aRRT6nnf' was not found on this server.
I made following changes to accomplish this:
//core.php
Configure::write('Routing.prefixes', array('aRRT6nnf'));
//routes.php
Router::connect('/aRRT6nnf', array('controller' => 'dashboard', 'action' => 'index', 'prefix'=>'aRRT6nnf', 'aRRT6nnf'=>true));
Any help would be appreciated.
Have you changed the prefixes on your controller methods
When using admin routing in Cake 1.3 your controller actions need to be prefixed with the route they pertain to for example take route admin your Dashboard controller should be something like this
class DashboardController extends AppController
{
public admin_index() {
}
So in your specific case you would need to change them to
public aRRT6nnf_index() {
}

Lithium PHP routes not working

I'm following the quickstart guide for Lithium: http://li3.me/docs/manual/quickstart
I created my posts Model in /var/www/my_app/app/models/Posts.php
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
}
?>
I created my posts Controller in /var/www/my_app/app/controllers/PostsController.php
<?php
namespace app\controllers;
class PostsController extends \lithium\action\Controller {
public function index() {
return array('foo' => 'bar', 'title' => 'Posts');
}
}
?>
And I created my View in /var/www/my_app/app/views/posts/index.html.php
Lithium is less dense than <?=$foo;?>ium.
The quickstart guide then says that I should be able to view my posts index page by going to
http://localhost/my_app/posts
but I get a
Not Found
The requested URL /my_app/posts was not found on this server.
However, if I go to just
http://localhost/my_app
the default home page that comes with Lithium is displayed.
So I tried fixing the problem by adding this line to my /var/www/my_app/config/routes.php file:
Router::connect('/posts', 'Posts::index');
But I get the same Not Found error?
You need to make sure that mod_rewrite is installed and enabled in your Apache.
Also check that the .htaccess file is present and allow_override is properly set for the virtualHost, otherwise .htaccess files will be ignored.
For further information, check the troubleshooting section in the documentation
A little bit further in the Routing documentation, the parameters of the Router::connect() method are explained more fully. The portion after the :: should be the name of the action called by the route; in your case index (or perhaps nothing, if there's a default set for the indexAction; not familiar with Lithium). You derive the "name" by removing the suffix Action from your Controller's method name. I suggest exploring the Routing documentation more fully to save yourself headaches in the future.

Categories