SEO Friendly Pretty URL for Blog, Any documentation or Easy Steps in Framework
- suresh_shinde
- Member | 3
For blog it is difficult to route for every URL manually.
Can we get post content & create user friendly URL(SEO URL) By using slug
instead of id in posts?
- Felix
- Nette Core | 1196
Hi. Thanks for the question.
In short way, yes, it's possible.
For example:
$router[] = new Route('blog/<post>', array(
'presenter' => 'Front:Post',
'action' => 'detail',
'post' => array(
Route::FILTER_IN => function ($post) {
return $this->postRepository->findBySlug($post);
},
Route::FILTER_OUT => function ($post) {
return $post->slug;
}
),
));
- suresh_shinde
- Member | 3
Hello Felix, thank you for your reply.
Fetching data from database by post_id is working.
I am facing issue with findBySlug function in postRepository. I am not able to get post data from database, I have two tables.
Master Table post having fields as;
id ,
gallery_id ,
user_id ,
image ,
show_in_homepage ,
created_at ,
updated_at
Detail table post_translation having fields as;
id ,
translatable_id ,
caption ,
perex ,
text ,
latte ,
file ,
slug ,
title ,
keywords ,
description ,
start_date ,
end_date ,
locale
Please help me to write function to get post data by slug instead id.
- CZechBoY
- Member | 3608
@suresh_shinde What database layer do you use?
For example in Nette\Database
class PostRepository
{
private $db;
public function __construct(Nette\Database\Context $db)
{
$this->db = $db;
}
public function findBySlug($slug)
{
return $this->db->table('post_translation')
->where('slug', $slug)
->select('post.*')
->fetch();
}
}
- suresh_shinde
- Member | 3
I found code for post class look like
namespace PostModule;
use Doctrine\ORM\Mapping as ORM;
use GalleryModule\Gallery;
use MYPS\Doctrine\BaseEntity;
use Kdyby\Doctrine\Entities\Attributes\Identifier;
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;
use TranslationModule\Translatable;
use UserModule\User;
/**
* @ORM\Entity
* @ORM\Table(name="blog")
*
* @author Petr Schefzu <petr.schefzu@myps.cz>
*
* @property Gallery $gallery
* @property User $user
* @property Category $category
* @property string $image
* @property bool $showInHomepage
*
* @method BlogTranslation translate($lang='')
*/
class Post extends BaseEntity
{
......
}