Solved

Cakephp replace id on url

Posted on 2010-08-19
3
400 Views
Last Modified: 2013-11-10
Hi all,

I am using cakephp and I am using a view to display records ... my code is very similar to this one:

http://book.cakephp.org/view/1536/Creating-Post-Views

My URL would be like: /view/1   where 1 is the id of the record to show.

My question is: How can I have the title field (got example) to show in the url insyead of the id?

Thanks

0
Comment
Question by:error77
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
ganesh_gns earned 500 total points
ID: 33482332
Hi,
Use the attach code it will solve your problem



<!-- File: /app/views/posts/index.ctp -->

<h1>Blog posts</h1>
<table>
	<tr>
		<th>Id</th>
		<th>Title</th>
		<th>Created</th>
	</tr>

	<!-- Here is where we loop through our $posts array, printing out post info -->

	<?php foreach ($posts as $post): ?>
	<tr>
		<td><?php echo $post['Post']['title']; ?></td>
		<td>
			<?php echo $html->link($post['Post']['title'], 
array('controller' => 'posts', 'action' => 'view', $post['Post']['title'])); ?>
		</td>
		<td><?php echo $post['Post']['created']; ?></td>
	</tr>
	<?php endforeach; ?>

</table>

---------------------------

<?php
class PostsController extends AppController {

	var $name = 'Posts';

	function index() {
		 $this->set('posts', $this->Post->find('all'));
	}

	function view($id = null) {
		$this->Post->title = $title;
		$this->set('post', $this->Post->read());
	}
}
?>

Open in new window

0
 

Author Comment

by:error77
ID: 33482680
Will try and get back to you.

Thanks!
0
 

Author Comment

by:error77
ID: 33490692
Hi ganesh_gns,

I tested your code but it didn't work.

Any ideas why?

thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now