Cakephp route all model to different name - php

I'm not quite sure how to do this, I'm not familiar enough with cakephp routing.
Basicly, I have model like: Contact and Project. However, after redesign and redevelop and using the same stuff but different term..
I need to change something like:
myapplication.com/contacts/add --> myapplication.com/customers/add
myapplication.com/contacts/edit --> myapplication.com/customers/edit
myapplication.com/projects/add --> myapplication.com/tasks/add
myapplication.com/projects/edit --> myapplication.com/tasks/edit
or any other view.
How should I do this?
I know standard way:
Router::connect('/dashboard', array('controller' => 'projects', 'action' => 'dashboard'));
Router::redirect('/projects/dashboard', '/dashboard');
I believe I can do it one by one, but that's not very intuitive.

Try this.
Router::connect('/customers/:action', array('controller' => 'contacts'));
Router::connect('/tasks/:action', array('controller' => 'projects'));
It might be also a good idea to read this section about routing. It is explained there in detail how to do it.

Related

Can't manage to create a static page with Zend Framework 2

I'm trying to make a website using the Zend Framework 2, but I have a simple problem driving me crazy.
I'd like to make a simple about-us.html page with static content because there is no need to do anything else than display html.
There is no need to create a Controller / model etc...
Maybe you have to question why you're using ZF in the first place. If you just want to create a static internet page, do that without a PHP framework!
If you didn't ask your question well and you're actually just adding a static page to an existing ZF application, why not just using the existing IndexController, add an empty action and add your static content to the corresponding .phtml?
Alternatively you can look at PhlySimplePage, a ZF2 module for the purpose of adding simple static pages to a ZF app. It's written by Matthew Weier O'Phinney, the lead dev of ZF.
As the readme says, this module eliminates the need to create a controller and an action. So all you need to do is create a route and a view script.
I know this question might be old, but I was in trouble with that as well.
Here is what I did to create a generic router for my application, so I could just add as many actions as I need and have then created as phtml files in my view
on module.config.php
'application' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action][/]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
So basically what it does is gets any action you might have in your application and use as a page... like any other controller
Instead of http://yoursite.com/application/test
you can do now http://yoursite.com/test
if you have your testAction set in your indexController file.
Hope I had helped future people looking for this information.
You have to create controller and view file.
suppose there is a about us menu with link
<a href="<?php $this->url('aboutus'array('action'=>'aboutus'));?>">
About Us
</a>
Just go to aboutus view file and write static html code.
Thanks
Alok

Cakephp Controller alias not working completely

I have a new question related to this question, but since the questions do not match at all, I am creating this new topic. But it is for good reference.
I changed my mind about the logs to log thing, logs is fine. But I am having another controller called WikisController, in this case I would really like to change the URL. I just hate it that the URL will be example.com/wikis/subject.html, I want it to be example.com/wiki/subject.html. It is much cleaner and the convention is now a 100% match with other sites where it is also called wiki, and not wikis. But, I want the controller to be called WikisController, because of the CakePHP convention.
Well, now comes the real problem. I was currently using the following routing in Config/routes.php:
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
Router::connect('/wiki/*', array('controller' => 'wikis'));
This is the piece of code used by the solution in the other topic. But now I am running into a little bit of trouble. Well, it isn't really a big issue because everything works just fine. But I don't like the URL.
When I create a link as the following from example.com/wiki/edit.html:
echo $this->Html->link('Wiki overview', array(
'controller' => 'wikis',
'action' => 'index',
'ext' => 'html'
));
It is creating a link to example.com/wiki/index.html totally fine and it works, but I don't want to show the index.html, I just don't. It is a word extra in the url and it is not necessary for the user to understand where he is.
I am creating the link with the controller and action key because of a little bug I was having earlier. When I didn't specify the action, it would create a link to example.com/edit.html which I don't want, obviously. So I have to add the action => index key to the array. I am not sure if this is a real bug, but it shouldn't matter. Index = index and nothing changes that. It is a good thing that I am sure my URL is pointing to the right page, so adding the action isn't an issue for me.
Just for good notice:
When I am creating a link on exactly the same way as I did above to example.com/flights/index.html from the page example.com/flights/add.html it would delete the /index part from the url and simply create a url to example.com/flights.html which is much cleaner.
So it has to do something with the routing, but I can't figure out what.
If I understood you correctly, then you'll need a route that connects /wiki to the controllers index action:
Router::connect('/wiki', array('controller' => 'wikis', 'action' => 'index'));
Router::connect('/wiki/:action/*', array('controller' => 'wikis'));
...

How can I have more than 2 directories deep with a controller in Kohana

I have a stat, in which many can exist for an improvement, which is one model in my about page. I initially built the page as one giant controller having silly actions like "action_editimprovementstat".
So I tried to move things into directories, so rather than everything being in "[...]/controller/about", I moved things into there perspective folders, for example: "[...]/controller/about/improvement/stat"
I changed the regex of the route, so the controller would accept slashes, which judging by the debugger, worked, because now the controller text will show up as "about/improvement/stat", unfortunately it still tells me the requested url can't be found.
So, I ask, what is the simplest way to have a hierarchical controller structure?
Here is an example of controller URLs that I would prefer:
/about
/about/internal
/about/external
/about/improvement
/about/improvement/stat
Those would also have actions, so for example:
/about/improvement/edit/6
/about/improvement/stat/delete/7
I'm willing to compromise if there are issues with ambiguity.
(Btw, I think I could manage a way if I did my own routing through a single controller, but I'm wondering if there is a better way, or if that way is well documented [so I can learn from another's experience].)
You can simply add additional variables or constant values to the route if you live.
The Kohana documentation even shows a concrete example, where an extra directory is added in front of the route, which can have only one of two given values:
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Of course you can add values in the back or inbetween as well. The only requirement is that your route will always result in at least a controller and an action. But they don't actually have to exist in the url. You can specify routes that match other values and have a constant value for controller and/or action, like this:
Route::set('search', ':<query>', array('query' => '.*'))
->defaults(array(
'controller' => 'search',
'action' => 'index',
));
The greatest pitfall: It is important to understand that routes are matched in the order they are added, and as soon as a URL matches a route, routing is essentially "stopped" and the remaining routes are never tried. Because the default route matches almost anything, including an empty url, new routes must be place before it.
Maybe that is what's going wrong now?
Anyway, rather than adding trickery to match slashes, I'd rather create a route that accepts a large number of optional variables, so you could read 'urlpart1' to urlpartX' from your generic controller. That is, if you need to. The setup, of course, is to let you create different controllers for different urls, so you don't need a humongous controller with a gigantic method to decide what to do based on the url parts.
Ever since I learned Kohana, my programming experience has been greatly improved because prior to Kohana I never gave a moments thought to how my urls were constructed. In the MVC world using Pretty URLs makes you really think about what you want to do and how to go about it.
In my opinion by looking at what you are wanting to do in the examples above, it seems to me that you are thinking backwards. You said the URLS that you preferred are: /about /about/internal /about/external /about/improvement /about/improvement/stat
It seems to me that "about" is really an action, not a controller. The url "/about/" is pretty confusing because it doesn't tell me what I'm getting information about but we can let that one slide because it's probably about the site in general. "/about/internal" is pretty clear but in a lot of ways you are trying to write your urls so that they read in proper English. In reality I would write them as: /about, /internal/about, /external/about, /improvement/about, /improvement_stat/about
I'm not sure why you are resisting have several controllers unless perhaps you are setting up your controllers as template controllers and maybe you think you have to do that for each one. You dont. In general I create a controller named "page" which is my template controller. Then all other controllers extend the page controller. I can define constants and other variables in the page controller that can be used in all the controllers that extend the page controller.
But if you are really resisiting writing multple controllers, you can always write specific routes that will let you reach any controller and action that you want. For example I used a route for a comparison where I wanted up to 4 id's passed into my route. I wrote that route like this:
Route::set('thing_compare', 'thing/compare/<thing1>/<thing2>(/<thing3>(/<thing4>))')
->defaults(array(
'controller' => 'thing',
'action' => 'compare'
));
Note that thing3 and thing4 are in parens which means they are optional. Then in my controller I can get those values by doing something like:
$thing1 = $this->request->param('thing1');
But going back to the examples you gave, just write the routes something like this (assuming your controller is named "about":
Route::set('about_internal', 'about/internal')
->defaults(array(
'controller' => 'about',
'action' => 'about_internal'
));
Route::set('about_external', 'about/external')
->defaults(array(
'controller' => 'about',
'action' => 'about_external'
))
Personally I would avoid setting routes like this and really reconsider how your urls need to be setup so that it creates a sensible design strategy.

CakePHP - routing with wildcards (controller not found error)

I'm not really even sure how exactly to search for this but I have a URL
site.com/forum/controller/action
Where forum is a plugin and I currently have it routing to the plugin forum successfully with
Router::connect('/forum', array('plugin' => 'forum', 'controller' => 'home', 'action' => 'index'));
However, I want to add a route that will connect any top-level subdirectory to the plugin forum. For example,
site.com/fish/controller/action
site.com/bird/controller/action
would both route to the forum plugin. Similarly,
site.com/bird
would also route to the forum plugin. This is the best I have been able to come up with and it has no effect (I get a "FishController could not be found":
Router::connect('/*/:controller/:action/*', array('plugin' => 'forum'));
The closest answer I could find basically says this might not be possible? http://cakephp.1045679.n5.nabble.com/Routes-with-wildcards-missing-controller-errors-td1263632.html
EDIT: After some more trial & error I tried this:
Router::connect('/:site/:controller/:action/*', array('plugin' => 'forum'));
And it works. Could someone explain this?
The documentation at http://api.cakephp.org/class/router#method-Routerconnect does a great job at explaining this.
What you've done is created a custom parameter. Cake uses an array to keep track of the parameters and that how it know which controller, action and other parameters have been passed. The Router will convert any URLs with 3 slashes (/) to $param['site'], $param['controller'] and $param['action'].
From your controller, you can retrieve the value of :site by using $this->params['site'].

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