CakePHP Router::url not working correctly - php

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

Related

Cakephp 3 - Routes being either an action or a parameter

I would like to know how to route the following scenario: I have a controller called Users, in this controller I have many actions, one of those is "profile".
I want my address being like that: mysite.com/users/NameOfTheUser OR mysite.com/users/edit-profile OR mysite.com/users/edit-photo, etc.
When you go for "edit-profile" you will be redirected to the edit_profile action, but here goes the trick, when you go to "NameOfTheUser" I want to redirect to the "profile" action, passing "NameOfTheUser" as a parameter.
Is there a way to do so without routing every action manually?
EDIT
I used the code that Yosi Azwan said, it works but I have to create a new route for each other page in the controller users.
Router::connect( '/users/:name', ['controller' => 'Users', 'action' => 'profile'], ['pass' => ['name']] );
Maybe this is what you are looking for
Router::connect( '/users/:name', ['controller' => 'Users', 'action' => 'profile'], ['pass' => ['name']] );
and read this for complete documentations http://book.cakephp.org/3.0/en/development/routing.html

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 : What is wrong with my pagiantion and routing?

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

Zend - outputting better module url?

I am currently outputting the url of a particular module like so:
$this->view->url(array('controller'=>'index','action'=>'index','module'=>'somemodule'))
The problem is when I view:
/somemodule/action1/paramid/paramval
The url is showing as:
/somemodule/index/index/paramid/paramval
When all I really want is:
/somemodule/
Any ideas? I have used the above url array because I am trying to force it to display the correct url. sadly, simply array('module'=>'somemodule') doesn't work...
Weird, I would've thought it defaults to index/index if they aren't specified. You could try setting up something with Zend_Controller_Router_Route;
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
You will however need to set up routes to capture the :id/:val into variables. You can do this with another addRoute(), naming each variable prepending a colon.
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule/:id/:val',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
Then you should be able to view the module without the index/index in the URL which might fix the problem with the url helper.
More info here http://framework.zend.com/manual/en/zend.controller.router.html

Categories