Cakephp : What is wrong with my pagiantion and routing? - php

I am using cakephp 1.3.X . I am experiencing some problem with url routing and pagination
My URL will look like
http://project.dev/search/hamburg/place:1
Below is my Routing code
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'));
Below is controller where I am coming to the search page
$this->redirect(array('controller'=>'searches','action'=>'index','slug'=>$slugUrl,$query));
My problem is when I go to the next page ,that won't showing the place:1 , just linking to search/hamburg/page:2 ie. place is missing
My index.ctp paginator options are given below
$paginator->options = array(
'url'=>array(
'controller'=>'searches',
'action'=>'index',
'slug'=>$this->params['slug'],
));
What going wrong with me. I've had searched a lot in cake articles , but nothing works for me. Please advise me

Add.
Router::connectNamed(array('slug'));
At the top of your router.php
Or define third pass param in your route, to pass slug.
Router::connect('/search/:slug/*', array('controller' => 'searches', 'action' => 'index'), array('pass'=>array('slug')));
Update view
$paginator->options = array('url' =>
array_merge(array('slug' => $this->params['slug']), $this->passedArgs)
);

Related

Is it possible to dynamically change view name or create a view that does not exist yet in phalcon?

I would like to know how can i do this in phalcon. I have a web site build with phalcon. All is working great now i stumbled upon a problem, here is what i need.
When a user clicks on a post that was created by another user. It takes him to this post with pictures and all things he entered to DB. I would like that in browser the name of this view is not like www.website.com/posts/index but that it is like www.website.com/posts/Nameofthepost, and like that for each other postings on the website. So that all posts (really ads) show their name up in browser. I hope i wrote everything understandable.
Appreciate all suggestions
That has to do with routing doesn't it? I modified this from my own code, I used grouping, you don't have to. I didn't test this code.
// routes.php
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule("__YOUR_MODULE__");
$router->removeExtraSlashes(true);
... your other routes ...
// posts group
$posts = new \Phalcon\Mvc\Router\Group(array(
'module' => '__YOUR_MODULE__',
'controller' => 'posts',
'action' => 'index'
));
// All the routes start with /post
$posts->setPrefix('/post');
$posts->add('/{postName}/:params', array(
'action' => 'index',
'params' => 2
));
// Maybe this will be enough for your needs,
// the one above has a catch all params, which
// has to be manually parsed
$posts->add('/{postName}', array(
'action' => 'index',
));
$posts->add('[/]*', array(
'action' => 'index',
));
$router->mount($posts);
unset($posts);
... other routes ...
return $router;
On your controller, you can get the postName param this way:
$this->dispatcher->getParam('permaPath');
As shown in the phalcon routing documentation, you can use regex in your routing config, something like this?
$posts->add('/{postName:[-0-6_A-Za-z]+}/:params', array(
'action' => 'index',
'params' => 2
));
So, only -_, 0-9, A-Z, a-z allowed for postName. If the URL had a comma in there or something, then route doesn't match, 404 page not found.

CakePHP - How to link to to a file that I created and doesn't come with the package

I am trying to make changes on a CakePHP cms site. I found "main_menu.cpt" file where is the main menu located. Following the existing menus I added my own:
<li>
<a href="<?=$html->url('/'.$lang.'/orders');?>"<?=($page=='orders')?'class="active" ':''?>>
<?__('orders')?>
</a>
</li>
which is pointing to file orders.php but when I click on the link I get this message:
Not Found
Error: The requested address '/en/orders' was not found on this server.
Where I must upload the file orders.php?
I know it sounds stupid, but this cms is totally new for me and even the directory structure doesn't help me :) Hope you will do it !
Thanks in advance
edit ################
<?php
Router::connect('/', array('controller' => 'dpages', 'action' => 'home', 'lang'=>'bg'));
Router::connect('/:lang/', array('controller' => 'dpages', 'action' => 'home'), array( 'lang' => 'bg|en'));
Router::connect('/:lang/pages/:action/*', array('controller' => 'dpages'), array( 'lang' => 'bg|en'));
Router::connect('/:lang/:controller/:action/*', array('action' => 'index'), array( 'lang' => 'bg|en'));
Router::connect('/pages/:action/*', array('controller' => 'dpages'));
Router::connect('/dpages/*', array('controller' => 'dpages', 'action' => 'view'));
Router::connect('/admin', array('admin'=>1, 'controller' => 'dpages', 'action' => 'home'));
Router::connect('/tests', array('controller' => 'tests', 'action' => 'index'));
?>
CakePHP is a framework.
You may need an OrdersController that has a view file for each method.
Check to see how the other pages are build (what files you have in the Controllers folder).
If you just want to add a content page, for orders, you can put it inside the View/Pages folder, and call it orders.ctp.
You can access it on site.com/pages/orders
Add the following line in Config/routes.php (among the other Router::connect lines)
Router::connect('/:lang/orders', array('controller' => 'dpages', 'action' => 'orders'), array( 'lang' => 'bg|en'));
Go to Controller/DPagesController.php and add:
public function orders() {
// can be blank for now
}
Go to views/dpages folder and create orders.ctp and put the static form in there.
Then try the link again.
CakePHP is not designed for you to add additional php files like that, nor should it be. Therefore I highly recommend that you rewrite whatever you have in your orders.php file to be use the CakePHP framework.
However, you can add orders.php into your app/Vendor folder, then include it in a controller's action (example: include_once(APP . 'Vendor/orders.php'); ). Then, you should be able to access it via http://yourwebsite.com/controller/action where controller is the name of the controller you chose to put the include in, and action is the name of the action you chose to put the include in.

CakePHP Router::url not working correctly

I'am using CakePHP 2.0 for a small admin section of a website.
If the user is logged in correctly, I redirect them to the admins dashboard.
I do this as following:
$this->redirect(Router::url(array('controller' => 'admin', 'action' => 'dashboard')));
The redirect is done correct, but for some reason the URL where it redirects to isn't correctly build. The URL is in the following structure (note the double [root] sections in the URL - this is what is wrong):
http://localhost/[root]/[root]/admin/dashboard
Off course their are errors shown because this controller / action doesn't exists. The URL should be off this form:
http://localhost/[root]/admin/dashboard
I can't seem to find what the exact problem is since cakePHP is not my core dessert, is there anyone that can point me in the right direction?
Thanks!
$this->redirect("YOUR URL");
example $this->redirect('/admins/dashboard');
This way you can redirect easily!
You can use the 'full_base' parameter in Router::url.
e.g.
$url = Router::url( array(
'controller' => 'posts',
'action' => 'view',
'id' => $post[id],
'full' => true
) );
This will give you a full base url. I found this fixed routing problems when I was running CakePHP in a subdirectory of localhost.
you can simply right like this
$this->redirect(array('controller' => 'admins', 'action' => 'dashboard'));
You can simply right this
Router::connect('/', array('controller' => 'users', 'action' => 'index'));

Remote controller name from CakePHP URL

I have a problem with the url rewriting.
The problem that I am facing is that currently our urls are like this:
http://www.xyz.com/sc_users/index
I dont want the controller name to be shown in that url.
Is there a way to achieve that??
First of all thank your guys..
Like I have 8 controllers I dont want the controller name to be shown in my url....this is what I want..
To be more precise no controller name in my url
You can define custom routes in app/config/routes.php. You'll find the all about routes in the CakePHP cookbook under Defining Routes. For example, a custom route can look like this:
Router::connect(
'/the_url_you_want_to_use/*', array('controller' => 'sc_users', 'action' => 'index')
);
You need to read up about CakePHP routing, look at the examples under 'defining routes'. Update your question with what you would actually like your URLs to look like and we will be able to help you more effectively.
That's simple :
there is a file called routes.php in /config directory :
You can do url rewriting stuff there like this :
Router::connect('/pages/*', array('controller' => 'cmsPage', 'action' => 'render'));
You can pass more complicated variables to your controller :
Router::connect('/:id-:lang-:profile-:firstName-:lastName-:profile.htm',
array('controller' => 'profiles','action' => 'view'),
array('id'=>'[0-9]*', 'lang'=>'fr','firstName'=>'[^-]*','lastNAme'=>'[^-]*','profile' => $util->keywords['profiles'][0]['fr'], 'pass' => array('id', 'lang'),'profile' => $util->keywords2['profiles'][0]['en'])
)
;
As you can see in the last example I have passed 2 parameters to the controller through 'pass' => array('id', 'lang')

How to create custom urls in cakephp

I have created a cakephp app. I have urls looks like
www.mysite.com/products/search/hardware
It loads fine. But I want urls that looks like
www.mysite.com/hardware
Can this is achieved by setting route connect
I appreciate any help.
Thanks.
Yes, you can use a route.
Router::connect(
'/hardware',
array('controller' => 'products', 'action' => 'search', 'hardware')
);
For a more general solution (any category name routing to products/search) see http://book.cakephp.org/view/948/Defining-Routes
You could add something like
Router::connect('/hardware',
array('controller' => 'products', 'action' => 'search'),
array('pass' => array('search'), 'search' => 'hardware'));
in your routes.php file, but then you would have to do it for each searchable item.
The problem you will be facing if you want something automatic, is that you need a way to differentiate your searchable products from every other model you have. So maybe you should settle for another type of URL like
www.mysite.com/products/hardware
or
www.mysite.com/s/hardware
and use the appropriate routes accordingly.
How about creating hardware_controller in your controllers folder.

Categories