CakePHP 1.3: Use Route to get the following URL format /news/06/27/12/slug-slug-slug - cakephp

All,
I currently have slug setup in my app where I generate the following as links:
http://www.domain.com/article/my-first-news-article.
And this is the current route that I use to accomplish that in addition to the view acepting a slug instead of id:
Router::connect('/article/*',array('controller' => 'articles', 'action' => 'view'));
However, I wanted to improve that a little bit more by adding published date to the URL like the following:
http://www.domain.com/article/2012/06/27/my-first-news-article
I have the following code from the CakePHP manual, but it doesnt seem to work:
Router::connect(
'/article/:year/:month/:day/:slug',
array(
'controller' => 'articles',
'action' => 'view'
),
array(
'year' => '[12][0-9]{3}',
'month' => '0[1-9]|1[012]',
'day' => '0[1-9]|[12][0-9]|3[01]'
)
);
For the sake of this, I really dont care what date is passed to the view. I just care about the slug that is passed. Then I will use function view($slug) to find the article and display it. However the URL needs to be http://www.domain.com/2012/06/27/slug-slug-slug
Thank you....

How do the $this->Html->link() calls look like in the places where you want to use the date in the link?
Are you adding the required params there?
$this->Html->link('Article', array('year' => 2012, 'month' => 01, 'day' => 01, 'action' => 'view', 'slug' => $article['Article']['slug']));

For the sake of completeness, and because somebody else might be interested in this as well, I'll show you my solution to a similar problem:
I want to have URLS in the format
http://mydomain.com/blog/2012/06/slug-slug-slug
i.e. /blog/yyyy/mm/slug
For this, I use
// view a post by year and month and title
Router::connect('/blog/:year/:month/:title/*', array(
'controller' => 'posts',
'action' => 'view'
), array(
'year' => '[12][0-9]{3}',
'month' => '0[1-9]|1[012]',
'title' => '[a-z0-9-]+'
));
With the rules below, you can make sure the correct formats are entered. It also prevents users from entering values such as &"% as slugs.
The /* after title even allows for additional parameters, e.g. for pagination, when your blog entry has several pages (http://mydomain.com/blog/2012/06/slug/page:2).
I also realized that setting the "pass" array is not really necessary (at least in CakePHP 2.0). You can access the parameters directly via $this -> request -> params['year'], etc.

After more research and trials I was able to figure it out.
The following route will work to accept a URL like this:
http://www.mydomain.com/article/2009/06/10/my-first-article-in-mydomain-dot-com.
As a matter of fact, it doesnt actually matter how the date is setup and it will still work. It could also be,
http://www.mydomain.com/article/06/10/2012/my-first-article-in-mydomain-dot-com
Router::connect(
'/article/:year/:month/:day/:slug',
array(
'controller' => 'articles',
'action' => 'view'
),
array(
'pass' => array('slug')
)
);

Related

How to redefine the URLs generation without changes at the HtmlHelper#link(...) calls in CakePHP?

I have a CakePHP website with many internal links, that are build with the HtmlHelper:
/app/View/MyController/myaction.ctp
<?php
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
$profileId,
$languageId
)
);
?>
It works fine with the default route:
/app/Config/routes.php
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
The generated links look like that: /mycontroller/myaction/$profileId/$languageId.
Now I want to use search engine friendly URLs (with profile names and ISO-639-1 language codes instead of IDs) for a part of the website and added a new Route:
/app/Config/routes.php
Router::connect(
'/:iso6391/:name.html',
array('controller' => 'mycontroller', 'action' => 'myaction'),
array(
'iso6391' => '[a-zA-Z]+',
'name' => '[0-9a-zA-ZäöüßÄÖÜ\-]+',
)
);
And it also works fine and the incomming URIs like /producer/en/TestName.html are interpreted correctly.
But the HtmlHelper is still generating the old URIs like /mycontroller/myaction/1/1.
The docu says:
Reverse routing is a feature in CakePHP that is used to allow you to easily change your URL structure without having to modify all your code. By using routing arrays to define your URLs, you can later configure routes and the generated URLs will automatically update.
Well, the HtmlHelper gets a routing array as input, that means: I'm using the reverse routing.
Why does it not work? How to make the HtmlHelper generate the new URLs (without changing the HtmlHelper#link(...) calls)?
Bit of explanation first
You are technically not using reverse routing. You see, the output link, /mycontroller/myaction/1/1 definitively doesn't match /iso/name.html. Like, in no way. So, the routing skips that rule because it doesn't apply.
Code
Try this
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
'iso6391' => $someStringWithIso,
'name' => $someName
)
);
But for that, you have to change your routing a bit, because you are not passing the parameters (check the docs for examples)
Router::connect(
'/:iso6391/:name.html',
array('controller' => 'mycontroller', 'action' => 'myaction'),
array(
'pass' => array('iso6391', 'name'),
'iso6391' => '[a-zA-Z]+',
'name' => '[0-9a-zA-ZäöüßÄÖÜ\-]+',
)
);
And you have to mind the first string match /:iso6391/:name.html. Do you want to match this route to every controller and action in your project, or just the one controller and the one view. If it is for all projects, just for precaution, use this
/:controller/:action/:iso6391/:name.html
if is just for, say, Controller1 and action "view", use
/controller1/view/:iso6391/:name.html
The detail you need to consider is the extension you use .html, is that really necessary in the url? If it is, add it as a parameter in the Html#link
echo $this->Html->link(
$item['Search']['name'],
array(
'controller' => 'targetcontroller',
'action' => 'targetaction',
'iso6391' => $someStringWithIso,
'name' => $someName
'ext' => 'html'
)
);
and also add parseExtensions to the routing file. Read this. Would be easier if you don't add the extension, but that's up to you.
In the end, you still have to change your calls to Html->link...

CakePHP - authentication component, using a different table name and column name

Just for organization sake, I wanted to use a different table for the authentication component to check, but it doesn't quite work. While I can initially state:
$this->Auth->userModel = "CoreUsers" plus set the loginAction to my proper MVC
works to look at that table just to confirm it's there, but the login doesn't work, it only keeps returning an incorrect password. Something happens in the authentication component; I can't tell what makes it fail. When I rename my table to "Users", it works.
The other part is I'd prefer to actually use the column name of 'email' rather than 'username' since that's really what I'm using anyway.
I am just not having luck finding a complete tutorial and reference sets to do both these successfully with CakePHP 2.x. What is the way forward?
References:
Stack Overflow question How do I use a table other than "Users" for CakePHP's AuthComponent?
Stack Overflow question CakePHP - 'AuthComponent' with a different model name (not 'User')
(I had a look for answers, but I never quite got the whole answer.)
Make sure your database table "core_users" and model "CoreUser" exists.
When you setup component you can put login/logout redirect here.
var $components = array(
"Auth" => array(
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'core_users', 'action' => 'login')
),
"Session");
Now in beforeFilter medhod you can put the following
function beforeFilter(){
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'CoreUser', 'scope' => array("CoreUser.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
);
}
Above example you can ignore status, if you need to pass any other info on the login verification u can use that. Now about you can remove 'Basic' if you only need form validation.
I hope this would work .
First, model names are generally singular. Are you sure you didn't mean CoreUser which would look in the core_users table?
Secondly, you can use the email field instead of username by setting the fields key on your auth setup.
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'CoreUser',
'fields' => array('username' => 'email')
)
)
)
);
See the book for more information.

CakePHP Routing - [home]/slug as URL

I'm trying to get to grips with Cake's routing, but I'm having no luck finding a solution to my particular problem.
I want to map www.example.com/slug to www.example.com/venues/view/slug, where slug is the URL friendly name of a record for a particular venue.
I also want to then map www.example.com/slug/events to www.example.com/events/index/venue:slug.
After reading the CakePHP documentation on Routing, a few times over, I'm none the wiser. I understand how I would create these routes for each individual venue, but I'm not sure how to get Cake to generate the routes on the fly.
You want to be careful mapping something to the first path after the domain name. This means that you would be breaking the controller/action/param/param paradigm. You can do this, but it may mean that you need to define every url for your site in your routes file rather than using Cake's routing magic.
An alternative may be to use /v/ for venues and /e/ for events to keep your url short but break up the url for the normal paradigm.
If you still want to do what you requested here, you could do something like the following. It limits the slug to letters, numbers, dashes, and underscores.
Router::connect(
'/:slug',
array(
'controller' => 'venues',
'action' => 'view'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
)
);
Router::connect(
'/:slug/:events',
array(
'controller' => 'events',
'action' => 'index'
),
array(
'slug' => '[a-zA-Z0-9_-]+'
)
);
In your controller, you would then access the slug with the following (using the first route as an example).
function view(){
if(isset($this->params['slug'])){
//Do something with your code here.
}
}
First off, you're not connecting www.example.com/slug to www.example.com/venues/view/slug, you're connecting /slug to a controller action. Like this:
Router::connect('/:slug',
array('controller' => 'venues', 'action' => 'view'),
array('pass' => array('slug'));
To generate the appropriate link, you'd do the same in reverse:
$this->Html->link('Foo',
array('controller' => 'venues', 'action' => 'view', 'slug' => 'bar'))
That should result in the link /bar.
The problem with routing a /:slug URL is that it's a catch-all route. You need to carefully define all other routes you may want to use before this one.

how to use i18n for one language cakephp

I need to launch a web app in spanish for the moment and I need to translate the app...
I already modified the default.po and added configure::write('Config.language', 'es') to the core.php...
what now? I don't want to add routing rigth now. Any suggestions?
PD: did everything as it is in the manual and ##$%^&%$## I cant get it to work
i18n is a tricky one to get your head around. If you're producing a website that will just be in Spanish, there is no need to use it, but I do use po messages as a matter of course, just in case.
There is a component that will help you a lot: http://bakery.cakephp.org/articles/p0windah/2007/09/12/p28n-the-top-to-bottom-persistent-internationalization-tutorial
There also used to be a script that would allow translation of slugs so that SEO would direct you to the right language. Last time I looked, it had vanished, but I'll try to piece it together for you.
For the moment, this is what I used in router.php
//route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));
//forgiving routes that allow users to change the lang of any page
Router::connect('/eng?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'en-gb'
));
Router::connect('/ca?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'cat'
));
Router::connect('/es?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'es_es'
));
I'll dig around for the url translation, but it may take a while....

Creating meaningful add URLs in Cakephp

For my site I have a number of Orders each of which contains a number of Quotes. A quote is always tied to an individual order, so in the quotes controller I add a quote with reference to it's order:
function add($orderId) {
// function here
}
And the calling URL looks a bit like
http://www.example.com/quotes/add/1
It occurred to me the URLs would make more sense if they looked a bit more like
http://www.example.com/orders/1/quotes/add
As the quote is being added to order 1.
Is this something it's possible to achieve in CakePHP?
Have a look at the documentation for defining routes.
Something like this should do the trick:
Router::connect(
'/orders/:id/quotes/add',
array('controller' => 'quotes', 'action' => 'add'),
array('id' => '[0-9]+')
);
You will be able to access the ID with $this->params['id'] in QuotesController::add().
Edit:
Also, have a look at the documentation for passing parameters to action.
It is possible to pass the ID in as a parameter of the controller action like so:
Router::connect(
'/orders/:id/quotes/add',
array('controller' => 'quotes', 'action' => 'add'),
array('pass' => array('id'), 'id' => '[0-9]+')
);
You can then access the ID with $id in QuotesController::add($id).

Resources