cake php default routing not work - cakephp

I want to define master routing for cakephp somthing like this.
Router::connect('/:lang/:plugins/:controller/:action/*', array('lang' => 'eng', 'controller' => 'index', 'action' => 'index', 'plugin' => null), array('lang' => '[a-z]{3}'));
but default not works. when i type these errors disappear:
/ Error: Controller could not be found.
/eng Error: EngController could not be found.
/eng/pages Error: EngController could not be found.

What the route errors are saying is that the controller that you're asking it to use doesn't exist. I'm assuming you're using CakePHP 2, so make sure your controller filename is like this:
EngController.php
And inside that controller file it is similar to this:
<?php
App::uses('AppController', 'Controller');
/**
* Eng Controller
*
*/
class EngController extends AppController {
/**
* Scaffold
*
* #var mixed
*/
public $scaffold;
}
EDIT:
I had another look at the route, and you have 'lang' => 'eng' at the start of the route. The errors you're getting are from the route thinking that eng is a controller. Remove this section altogether and it should fix it.

Related

Create plugin cakephp and use it in browser

I'm trying to create a plugin, this is my folder layout;
in bootstrap.php
CakePlugin::load('ContactManager');
in routes.php
Router::connect(
'/ct/',
array(
'plugin' => 'ContactManager',
'controller' => 'Contacts',
'action' =>'index'
)
);
ContactManagerAppController.php
<?php
class ContactManagerAppController extends AppController {}
ContactsController.php
<?php
class ContactsController extends ContactManagerAppController {
public $uses = array('ContactManager.Contact');
public function index() {
//...
}
}
ContactManagerAppModel.php
<?php
class ContactManagerAppModel extends AppModel {}
Contact.php
class Contact extends ContactManagerAppModel {}
How do I display index.ctp in my browser
index.ctp
<?php echo 'hello'; ?>
Requesting http://localhost/cakephp/ct/ContactManager/Contacts gives a missing controller error, instead of my plugin controller index:
You should be able to access your plugin's Contacts Controller (without setting up routes) from /contact_manager/contacts. So by the looks of your question the absolute URL should be:-
http://localhost/cakephp/contact_manager/contacts
If this is not working then there's something wrong with the plugin's setup. Make sure the plugin files are readable and that the filenames and classes are all correct. Your plugin structure looks good otherwise.
If the above URL works then (and only then) you can consider re-routing it. To do this you need to do something like this (preferably in /app/Config/routes.php):-
Router::connect(
'/ct/',
array(
'plugin' => 'contact_manager',
'controller' => 'contacts',
'action' =>'index'
)
);
If the route does not work try checking that nothing is overridding it elsewhere in your routes file.
You should use snake case (e.g. 'contact_manager') not camel case (e.g. 'ContactManager') in your route parameters.
This is my answer ---> thanks all
http://i.imgur.com/Ju8mq4D.jpg
in Controller:
AppController.php
ContactsController.php
in Model
AppModel.php
Contact.php
in View/Contacts
index.ctp
app/Config/routes.php
Router::connect('/ct', array('plugin'=>'ContactManager', 'controller' => 'contacts', 'action' => 'index'));
app/Config/bootstrap.php
CakePlugin::load(array('ContactManager' => array('bootstrap' => true, 'routes' => true) ));

Plugin route to a prefixed controller

I'm creating a plugin for my application (using CakePHP 2.6.0) that allows users to login into a user area using the same model as for the admin area, so I'm trying to get the same type of URI scheme as the admin area e.g. /admin/users/login but then for /special/users/login. I have the following route in my plugin's Config/routes.php and added the 'special' prefix to the 'Routing.prefixes' configuration:
Router::connect('/special/:controller/:action', array(
'special' => true,
'prefix' => 'special',
'plugin' => 'Special',
'controller' => ':controller',
'action' => ':action',
));
With above route and entering the following url /special/users/login it would make sense to me right now if Cake went for Plugin/Controller/SpecialUsersController.php (considering namespace conflicts with the main application's UsersController) but instead I get an error Error: Create the class UsersController.
Is there a built-in way for it to load a prefixed controller (without changing the url) based on the plugin? Or is there a better way to neatly extend my main application? Am I going about this the wrong way?
I could not find a built-in way for it to work as I wanted to so I changed the way URL strings were parsed using a custom route class in my app's Routing/Route/ folder:
App::uses('CakeRoute', 'Routing/Route');
/**
* Plugin route will make sure plugin routes get
* redirected to a prefixed controller.
*/
class PluginRoute extends CakeRoute {
/**
* Parses a string URL into an array.
*
* #param string $url The URL to parse
* #return bool False on failure
*/
public function parse($url) {
$params = parent::parse($url);
if($params && !empty($params['controller']) && !empty($params['plugin'])) {
$params['controller'] = $params['plugin'] . ucfirst($params['controller']);
}
return $params;
}
}
And then setting up my plugin routes as:
App::uses('PluginRoute', 'Routing/Route');
Router::connect('/special/:controller/:action', array(
'special' => true,
'prefix' => 'special',
'plugin' => 'Special',
'controller' => ':controller',
'action' => ':action',
), array(
'routeClass' => 'PluginRoute'
));
This resulted in /special/users/login creating the controller I wanted Plugin/Controller/SpecialUsersController.php, no side effects so far. Extending the main application's UsersController is a different story though.
Maybe someone else has use of this or knows a better solution?

CakePHP Plugin - Error "Controller could not be found"

I'm currently using CakePHP 2.5.2
I want to integrate a plugin to manage Users.
I tested it in a first time as a single CakePHP Application (controllers in app/controller, models in app/model, views in app/view/Users): was ok.
I'm trying now to convert it as a plugin:
I've created a folder UserManager in app/plugin.
When I try to go to the url of one of my controllers, I get the message Missing Controller.
All plugin are loaded in Bootstrap.php (CakePlugin::loadAll();).
I tried to find similar problems vs solutions but no one was relevant with my problem (I tried some proposed solutions but root causes were different.
When I look at DebugKit in Include section, I can observe that my DebugKit plugin is loaded but not my other plugin...
Could some one suggest me a solution ?
Thanks in advance.
(Please find bellow a description of the code)
I added controllers, models and views as follows (skeleton generated by Bake and checked: ok):
1) Models in app/plugin/model
UserManagerAppModel.php
<?php
App::uses('AppModel', 'Model');
class UserManagerAppModel extends AppModel {
}
?>
User.php
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends UserManagerAppModel {
...
}
?>
2) Controllers in app/plugin/controller
UserManagerAppModel.php:
<?php
App::uses('AppController', 'Controller');
class UserManagerAppController extends AppController {
}
?>
UserController.php:
<?php
class UsersController extends UserManagerAppController {
public $uses = array('UserManager.User');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
}
public function login() {
}
...
?>
3)Views in app/plugin/view/Users
Nothing special.
I think it is an issue with your path names: They have to include the plugin name. Plus the folder names have to start with a capital letter by convention. For example, UserManagerAppModel has to be in the following file: app/Plugin/UserManager/Model/UserManagerAppModel.php.
See also http://book.cakephp.org/2.0/en/plugins.html#creating-your-own-plugins
Since I believe has included AuthComponent settings in your AppController loginAction note the call and check the name of your plugin. In my case the plugin name is Barracuda, the error does not occur stopped until the call was made ​​for barracuda, with the first letter in lower case.
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => 'Barracuda'
),
for
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => 'barracuda'
),

CakePHP3.x controller name in url when using prefix routing

I am trying to use prefix routing in CakePHP3. I added the following lines to /config/routes.php.
Router::prefix("admin", function($routes) {
    // All routes here will be prefixed with ‘/admin‘
    // And have the prefix => admin route element added.
    $routes->connect("/",["controller"=>"Tops","action"=>"index"]);
    $routes->connect("/:controller", ["action" => "index"]);
    $routes->connect("/:controller/:action/*");
});
After that, I created /src/Controller/Admin/QuestionsController.php like below.
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class QuestionsController extends AppController {
public function index() {
//some code here
}
}
?>
Finally I tried to access localhost/app_name/admin/questions/index, but I got an error saying, Error: questionsController could not be found. However, when I capitalize the first letter of controller name(i.e. localhost/app_name/admin/Questions/index), it is working fine. I thought it is weird because without prefix, I can use controller name whose first character is not capitalized.
Is this some kind of bug?
In Cake 3.x, routes do not inflect by default anymore, instead you'll have to explicitly make use of the InflectedRoute route class, as can for example bee seen in the default routes.php app configuration:
Router::scope('/', function($routes) {
// ...
/**
* Connect a route for the index action of any controller.
* And a more general catch all route for any action.
*
* The `fallbacks` method is a shortcut for
* `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);`
* `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);`
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks();
});
You custom routes do not specify a specific route class, so the default Route class is being used, while the fallback routes make use of inflected routing, and that is why it's working without prefix.
So either use capitalized controller names in your URL, or use a route class like InflectedRoute that transforms them properly:
Router::prefix('admin', function($routes) {
// All routes here will be prefixed with ‘/admin‘
// And have the prefix => admin route element added.
$routes->connect(
'/',
['controller' => 'Tops', 'action' => 'index']
);
$routes->connect(
'/:controller',
['action' => 'index'],
['routeClass' => 'InflectedRoute']
);
$routes->connect(
'/:controller/:action/*',
[],
['routeClass' => 'InflectedRoute']
);
});
See also http://book.cakephp.org/3.0/en/development/routing.html#route-elements

CakePHP 2.x custom "Authentication adapter "LdapAuthorize" was not found

I'm building an application using CakePHP and trying to incorporate a custom authentication object but it does not seem to be able to find it. I get the following error when I try to log in: "Authentication adapter "LdapAuthorize" was not found". I have created the file app/Controller/Component/Auth/LdapAuthorize.php with my code for my authentication. Near the top of "AppController.php" I have
App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');
and within the AppController class I have
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pendings', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array('Controller'),
'authenticate' => array('LdapAuthorize')
)
);
and then in my UsersController.php I have the following login function.
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
// My Login stuff...
}
else
$this->redirect(array('controller'=>'someController', 'action'=>'someAction'));
}
}
If anyone has any idea why it can't seem to load my custom authentication object that would be awesome. Thanks!
I put my custom authentication class inside Controller/Component/Auth. For example, the name of my class is CustomUserAuthenticate and the path to the file is,
Controller/Component/Auth/CustomUserAuthenticate.php.
Then in my AppController I added the following to the authenticate array,
class AppController extends Controller {
public $components = array(
'Auth' => array(
/** Any other configuration like redirects can go here */
'authenticate' => array(
'CustomUser'
)
)
);
}
The string in the authenticate array must match the name of the class except for the Authenticate word.
My CustomUserAuthenticate class extends CakePHP's Controller/Component/Auth/BaseAuthenticate and overrides the authenticate method. CakePHP's documentation states that this is not required. I haven't tried that way.
I think your App::uses() is wrong so it can't find the class. Your current code:
App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');
Is trying to find Controller/Component/Auth/LdapAuthorize/LdapAuthroize.php
The first parameter is the class name (you have a typo with that), the second is just the path to the directory containing the class, you don't need to add the class name again.
Try this:
App::uses('LdapAuthorize', 'Controller/Component/Auth');

Resources