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>
Related
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']);
I have two function "product" and "view" in cakephp. If any one type domainname.com/item1 then call "product" function and if domainname.com/item1/item2 then call "view" function .
item1 and item2 is dynamic content.
Router::connect('/:category', array('controller' => 'Posts', 'action' => 'product'));
Router::connect('/:category/:title', array('controller' => 'Posts', 'action' => 'view'));
I use this code in routes.php
Problem is this if I enter domainname.com/item1 then it is call view function.
please suggest me how to use url rewriting in cakephp .
try to add /posts and specifie the order like this:
Router::connect('/posts/:category/:title',
array('controller' => 'Posts', 'action' => 'view'),
array(
// order matters since this will simply map ":category" to $category in your action
'pass' => array('category', 'title')
)
);
You can take a look to the doc http://book.cakephp.org/2.0/fr/development/routing.html
try this
Router::connect('/:category/:product',
array('controller' => 'posts', 'action' => 'view'),
array('pass' =>
array('product')
));
Router::connect('/:product', array('controller' => 'posts', 'action' => 'product'));
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.
I've recently started a new job and I noticed that a page linked to in one of our mailing lists no longer exists. I'm a developer / sysadmin so I thought it would be simple to set up a redirect, however our site is running CakePHP on the backend which I've got no experience with.
I want to redirect oursite.com/page/online-store to just oursite.com
I've tried Router::redirect('/page/online-store', 'http://www.oursite.com', array('status' => 301)); at the bottom of that file but it doesn't seem to be working, it just hangs at page load until I comment that line out.
Relevant lines in routes.php:
// login and register
Router::connect('/login', array('controller' => 'accounts', 'action' => 'login'));
Router::connect('/register', array('controller' => 'accounts', 'action' => 'register'));
// home
Router::connect('/', array('controller' => 'site', 'action' => 'home'));
Router::connect('/admin', array('controller' => 'users', 'action' => 'login', 'admin' => true));
// dashboard
Router::connect('/admin/dashboard', array('controller' => 'pages', 'action' => 'dashboard', 'admin' => true));
// search
Router::connect('/search', array('controller' => 'products', 'action' => 'search'));
// pages
Router::connect('/page/site-map', array('controller' => 'site', 'action' => 'map'));
Router::connect('/page/home', array('controller' => 'site', 'action' => 'home'));
Router::connect('/page/*', array('controller' => 'pages', 'action' => 'index'));
I'd appreciate any help.
I have my app set up to go to Books index page
Here is my address
http://myapp.loc/
Here is router
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
When I go to http://myapp.loc/, the page loads up just fine. But when I click a link within that page that is suppose to link to http://myapp.loc/books/index, it doesn't work.
The web developer tools show
Remote Address:127.0.0.1:80
Request URL:http://myapp.loc/books/index
Request Method:GET
Status Code:404 Not Found
I tried adding a statement in books controller under index action
die("here is my breakpoint");
I see this when the app loads. But if I take it out and let the app load and then click on the link thats basically the same thing, it doesnt even hit that part of the code.
Any help will be appreciated.
Router
<?php
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
//Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources('books', array('id'=>'[0-9A-Za-z]'));
Router::parseExtensions('json');
require CAKE . 'Config' . DS . 'routes.php';
Try to add second line like
Router::connect('/', array('controller' => 'books', 'action' => 'index'));
Router::connect('/books/*', array('controller' => 'books', 'action' => 'index'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Check Manual Here