Php routing always contains Pages - php

Everytime i try and reference a file or anything, /Pages/ always is at the start of the URL. so i get these errors, and need to change all button links to ../
My routes.php file is as such
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('Pages/*', ['controller' => 'Pages', 'action' => 'display']);
I'm using cakephp inside codeanywhere (dont ask).

Try:
$routes->connect('/Pages/*', ['controller' => 'Pages', 'action' => 'display']);

Related

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.

Can i hide controller and view name cake php 2?

I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.

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

Kohana 3.3 controllers in subfolders

I have controllers
--controllers
--Administrator
-Base.php
-Admin.php (extend Base.php)
-controller1.php
-controller2.php
-etc
And my route looks like this
Route::set('administrator', 'Administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Try to load this controller and i get message Not found
What's wrong?
UPDATE!
class Controller_Administrator_Base extends Controller_Template {
public $template = 'panel/index';
public function action_index(){
echo 'kupakonia';
}
} // End Welcome
All routes. I was trying alot of sugesstions from google and nothing helps, and I write something becouse i cant update this tobic becouse is too much code inside.
Still can't update.
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Route::set('user', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Any sugestions?
Ps. still can't update this topic :/ Wtf must be more characters in tekst than in code ?
this is my first asc on stackoverflow. So sorry for this ;)
You have two catch all routes (default and user) in your Bootstrap file. These two routes do exactly the same.
Also, since these routes are catch all and the order of the routes matter, this means if the catch all route is the first it will match all URLs.
Try this:
Route::set('administrator', 'administrator(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Administrator',
'controller' => 'base',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'read',
'action' => 'index',
));
Another thing, it is usually bad practice to have a catch all route. Try to create routes specific for your Controllers.

Link for static page in CakePHP without appending the controller

So I created a couple of static pages in views > pages folder. They are contact.ctp and privacy.ctp. In my routes.php, I made it so that they could be viewed by going to domain.com/contact and domain.com/privacy with:
Router::connect('/contact', array('controller' => 'pages', 'action' => 'display', 'contact'));
Router::connect('/privacy', array('controller' => 'pages', 'action' => 'display', 'privacy'));
Now, when I link them at the footer with:
<li><?= $this->Html->link('Contact', array('controller' => 'pages', 'action' => 'display', 'contact')); ?></a></li>
<li><?= $this->Html->link('Privacy', array('controller' => 'pages', 'action' => 'display', 'privacy')); ?></a></li>
They are linked as domain.com/pages/terms. How can I stop it from appending the pages controller without giving an absolute url (i.e. without doing: <?= $this->Html->link('Contact', 'http://www.domain.com/contact'); ?> or is that the only other way?
you probably put these routes after Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); Just reverse that order and it should work.
ROUTE
Router::connect('/*', array('controller' => 'pages', 'action' => 'display'));
VIEW
echo $this->Html->link('Target', $this->Html->url(array('controller'=>'pages', 'action'=>'display', 'target', 'ext'=>'html')));
OUPUT
Target
Use an actual link?
Contact
And:
Privacy
Short and sweet ^_^
For SE posterity, and the sake of succinctness, you can use Router::url( ).
<li>Contact</li>

Categories