defined route is missing - Missing Route in Cakephp 3.4 - php

I am trying to use the prefix "student". When I create a link in template or layout file I am getting this error as shown in the image:
code in routes.php
<?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('admin', function ($routes) {
$routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
$routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false, 'prefix'=>'admin']);
$routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('trainer', function ($routes) {
$routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
$routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false]);
$routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('student', function ($routes) {
$routes->connect('/courses/', array ( 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ));
$routes->connect('/', array ( 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin' => false));
});
/**
* Load all plugin routes. See the Plugin documentation on
* how to customize the loading of plugin routes.
*/
Plugin::routes();
layout file student.ctp has only one line of code:
<li><?php echo $this->Html->link('Courses', [ 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ]);?></li>
AppController.php:
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
class AppController extends Controller
{
public $helpers = array(
'CakeDC/Users.AuthLink',
'CakeDC/Users.User',
);
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('CakeDC/Users.UsersAuth');
$this->loadComponent('Utils.GlobalAuth');
$this->Auth->config('loginRedirect', array('controller'=>'Courses', 'action'=>'index', 'plugin'=>FALSE));
$this->Auth->config('logoutRedirect', array('controller'=>'MyUsers', 'action'=>'login', 'plugin'=>FALSE));
$this->Auth->config('unauthorizedRedirect', array('controller'=>'Courses', 'action'=>'index', 'prefix'=>$this->Auth->user('role')));
$this->Auth->config('loginAction', array('controller'=>'MyUsers', 'action'=>'login'));
$this->Auth->allow(['login', 'logout']);
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
$this->_renderLayout();
}
private function _renderLayout()
{
$prefix = isset($this->request->params['prefix'])?$this->request->params['prefix']:FALSE;
if(!$prefix)
{
return;
}
$this->viewBuilder()->setLayout($prefix);
}
}
I have checked this solution : CakePHP 3: Missing route error for route that exists

You cannot feed the special plugin key with a boolean, it must either be null, or a string with the name of the plugin.
Also there is no need to define the plugin or prefix keys when connecting routes, the Router::prefix() method will take care of adding the prefix. Similarily Router::plugin() will add the plugin name, and when not using Router::plugin(), a default of null is being assumed for the plugin key.
Furthermore defining _ext with null only makes sense if you want to disallow generating URLs with extensions. And specifying it when generating URLs is only neccessary when it has been defined as a non-null value, which is also true for the plugin key (unless you need to break out of the current plugin context).
Long story short, connecting the route only requires the controller and action keys:
$routes->connect('/courses/', [
'controller' => 'Courses',
'action' => 'index'
]);
And generating the URL only needs the additonal prefix key, plugin is optional if not used in plugin context:
$this->Html->link('Courses', [
'controller' => 'Courses',
'action' => 'index',
'plugin' => null,
'prefix' => 'student'
]);
See also
Cookbook > Routing > Prefix Routing
Cookbook > Routing > Plugin Routing
Cookbook > Routing > Routing File Extensions

Related

Kohana more than one dynamic routing for specific urls

I don't know if someone else is using kohana (koseven with new name) framework for develepment. I need help about routing. I am migrating an asp site to php with using koseven ( kohana) framework and I must keep all the url routing on current site. Because of this I must use more than one routing on my project.
Url structer must be like this:
domain.com/contenttype/contentid -> contenttype is dynamic and gets data over Content Controller
domain.com/profile/username ->profile is the controller and index is the action. I must get the user name from id parameter.
domain.com/categories/categorname (Works fine-> categories is the controller, index is the action and categorname is the id parameter.
There is an admin page on my site and using a directory route on it.
Here is my route on bootstrap.php file:
Route::set('panel', '<directory>(/<controller>(/<action>(/<id>)))', array('directory' => 'panel'))
->defaults(array(
'controller' => 'panel',
'action' => 'index',
));
Route::set('kategori','<kategori>(/<id>)', array('id'=>'.*'))
->defaults([
'controller'=>'kategori',
'action'=>'index',
]);
Route::set('default', '(<controller>(/<action>(/<id>)))', array('id'=>'.*'))
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
]);
First Problem: If I copy kategori route for profile it uses kategori route instead of profile.
Second Problem: How can I get dynamic routing for the contenttype. Content controller is the default controller and it will list the contents under the dynamic contenttype if there isn't given any content title on the id parameter. If the id parameter is identified at this time it will Show the detail of content.
Thanks.
Route::set('panel', 'panel(/<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults(array(
'directory' => 'panel',
'controller' => 'dashboard',
'action' => 'index',
'id' => null,
));
Route::set('kategori','<kategori>(/<id>)', ['kategori' => '[-\w]+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'kategori',
'action' => 'index',
'id' => null,
])
->filter(function ($route, $params, $request) {
$model = ORM::factory('Kategori', ['kategori' => $params['kategori'], 'id' => $params['id']]);
if ($model->loaded()) {
$params['model'] = $model;
return $params;
}
return false;
});
Route::set('default', '(<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
'id' => null,
]);

Set Default Prefix in CakePHP 3 based on sub domain

I want to build multiple sub domain which point to same source code of CakePHP v3
Scenario is
If domain is "admin.localhost.com" then prefix value should be admin.
If domain is "xyz.localhost.com",'abc.localhost.com' or any on sub domain then prefix value should be vendor
If domain is "localhost.com" or "www.localhost.com" then prefix value should be false as cakephp 3 have by default.
I have tryied to findout from CakePHP 3 document. but I didint get how to set default prefix.
Thanks in Advance
I got Answer of my question myself
We have to set prefix in config/routs.php by exploding HTTP_HOST
$exp_domain= explode(".",env("HTTP_HOST"));
$default_prefix=false; // default prefix is false
if(count($exp_domain)>2 && $exp_domain[0]!="www")
{
if($exp_domain[0]=="admin") $default_prefix="admin";
else $default_prefix="vendor";
}
if($default_prefix=="admin")
{
// default routes for vendor users with base scope and pass prefix as admin ($default_prefix)
Router::scope('/', function ($routes) use($default_prefix) {
$routes->connect('/', ['controller' => 'admins', 'action' => 'dashboard','prefix'=>$default_prefix]);
$routes->connect('/:action', ['controller' => 'admins','prefix'=>$default_prefix]);
$routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]);
$routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]);
});
}
else if($default_prefix=="vendor")
{
// default routes for vendor users with base scope and pass prefix as vendor ($default_prefix)
Router::scope('/', function ($routes) use($default_prefix) {
$routes->connect('/', ['controller' => 'vendors', 'action' => 'dashboard','prefix'=>$default_prefix]);
$routes->connect('/:action', ['controller' => 'vendors','prefix'=>$default_prefix]);
$routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]);
$routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action','prefix'=>$default_prefix]);
});
}
else
{
// default routes for normal users with base scope
Router::scope('/', function ($routes) use($default_prefix) {
$routes->connect('/', ['controller' => 'users', 'action' => 'dashboard');
$routes->connect('/:action', ['controller' => 'users');
$routes->connect('/:controller/:action', ['controller' => 'controller', 'action' => 'action');
$routes->connect('/:controller/:action/*', ['controller' => 'controller', 'action' => 'action');
});
}
So main trick is need to pass prefix on root scope.

Cakephp 3 routing with language parameter

I'm trying to convert cakephp 2.x to 3.x. I was using Router::connect() rules, but I try to convert them to scope version.
Regarding to myold routing rule, in config/routes.php I added this.
Router::defaultRouteClass('Route');
Router::scope('/', function ($routes) {
$routes->connect('/:language/:controller/:action/*', ['language' => 'ar|de|en|fr']);
$routes->connect('/:language/:controller', ['action' => 'index', 'language' => 'ar|de|en|fr']);
$routes->connect('/:language', ['controller' => 'Mydefault', 'action' => 'index', 'language' => 'ar|de|en|fr']);
$routes->redirect('/gohere/*', ['controller' => 'Mycontroller', 'action' => 'myaction'], ['persist' => array('username')]);
$routes->connect('/', ['controller' => 'Mydefault', 'action' => 'index']);
$routes->fallbacks('InflectedRoute');
});
But this fails in example.com/en/works. I get this error: Error: worksController could not be found. Because my controller file is WorksController.php.
Does controller name part hanged to sentence casein cakephp 3 ? http://book.cakephp.org/3.0/en/intro/conventions.html#controller-conventions
Also example.com/foo/bar gives this error: Error: barController could not be found.. But foo is controller and bar is action.
How can I fix this routing problem ?
Edit:
Changing Route::defaultRouteClass('Route') to Route::defaultRouteClass('InflectedRoute') solved problem 1. But problem 2 exists.
Options, such as route element patterns, must be passed via the third argument of Router::connect(), the $options argument.
This route:
$routes->connect(
'/:language/:controller',
['action' => 'index', 'language' => 'ar|de|en|fr'
]);
will catch your /foo/bar URL, it will match foo for the :language element, and bar for the :controller element. Basically the language key in the URL array will be treated as the default value, and it will always be overwritten by the :language element value.
The correct way of defining the route is:
$routes->connect(
'/:language/:controller',
['action' => 'index'],
['language' => 'ar|de|en|fr']
);
The other routes need to be adapted accordingly.
See also Cookbook > Routing > Connecting Routes
The best way is using Routing scopes
<?php
$builder = function ($routes) {
$routes->connect('/:action/*');
};
$scopes = function ($routes) use ($builder) {
$routes->scope('/questions', ['controller' => 'Questions'], $builder);
$routes->scope('/answers', ['controller' => 'Answers'], $builder);
};
$languages = ['en', 'es', 'pt'];
foreach ($languages as $lang) {
Router::scope("/$lang", ['lang' => $lang], $scopes);
}
Router::addUrlFilter(function ($params, $request) {
if ($request->param('lang')) {
$params['lang'] = $request->param('lang');
}
return $params;
});
Code taken from:
https://github.com/steinkel/cakefest2015/blob/c3403729d7b97015a409c36cf85be9b0cc5c76ef/cakefest/config/routes.php
Extending on default router from CakePHP 3 application skeleton
original routes.php removed comments
<?php
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function (RouteBuilder $routes) {
$routes->applyMiddleware('csrf');
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks(DashedRoute::class);
});
modified with language from defined set
<?php
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
$routerCallback = function (RouteBuilder $routes) {
$routes->applyMiddleware('csrf');
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks(DashedRoute::class);
};
// support only for 3 languages, other language will throw 404/NotFoundException
// or will cause different routing problem based on your routes
Router::scope('/', $routerCallback);
foreach (["en", "fr", "de"] as $language) {
Router::scope('/' . $language, ['language' => $language], $routerCallback);
}
// to access the language param, or default to 'en', use
// $this->request->getParam('language', 'en')
// from AppController, PagesController, etc...
rooter.php
$routes->connect('/:lang/:controller/:action',[],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
$routes->connect('/:lang/', ['controller' => 'Pages', 'action' => 'index'],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
$routes->connect('/:lang/index', ['controller' => 'Pages', 'action' => 'index'],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
$routes->connect('/:lang/pages/*', ['controller' => 'Pages', 'action' => 'index'],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
$routes->connect('/:lang/contact', ['controller' => 'Pages', 'action' => 'contact'],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
$routes->connect('/:lang/about', ['controller' => 'Pages', 'action' => 'about'],[ 'lang' => '[a-z]{2}','pass' => ['lang']]);
Class Appcontroller
public function beforeFilter(Event $event)
{
$this->Auth->allow(['']);
if(isset($this->request->params['pass'][0]))
$lang = $this->request->params['pass'][0];
else $lang = 'en';
I18n::locale($lang);
}

Cakephp LogOut Not work

i have a big problem
i have 3 view folder {admin , teacher , user }
and in all of them use index.ctp , and logout.ctp
when i want to logout ,url redirect to /dashboard
my appcontroller.php
public $components = array(
'Cookie',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
));
// only allow the login controllers only
public function beforeFilter() {
$this->response->disableCache();
$this->Auth->allow('login','logout');
}
and my routes.php
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/dashboard', array('controller' => 'users', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
and use this function for logout in my controller
public function logout()
{
$this->redirect($this->Auth->logout());
}
pls help my to solve that.tnx
Simply replace this in your controller
public function logout()
{
$this->Auth->logout();
$this->redirect( '/dashboard' );
}
It will work perfectly.

Auth component in cakephp supports only UsersController?

I might hav askd question related to this earlier but not satisfied by answers and no answer is working.....My doubt is little different , i have two controllers
1.UsersController.
2.MembersController.
My doubt is the Auth component is working wonders for UsersControllers, but the Auth is not working for MembersController. In simple terms whenever i try to use Auth component for my MembersController, instead of redirecting to Members view. It is displaying UsersController pages....And when i delete the UsersController i get below error...
Error: UsersController could not be found.
Is there any connection between Auth and Users. How to set Auth component for my MembersController......
This is how i am using it....
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
In your App Controller
class AppController extends Controller {
public $components = array(
'Auth' => array(
'authorize' => 'actions',
'actionPath' => 'controllers/',
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
'plugin' => false,
'admin' => false,
),
),
);
}

Categories