Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding slug field to Blog entity. Updating blog show route to append …

…slug to URL
  • Loading branch information...
commit 1e53dd37aed38169fe5fe153e07f9dd8263042bf 1 parent 635875c
@dsyph3r authored
View
3  .gitignore
@@ -4,4 +4,5 @@ vendor/*
app/cache/*
app/logs/*
web/bundles/*
-app/DoctrineMigrations
+app/DoctrineMigrations
+*~
View
2  src/Blogger/BlogBundle/Controller/BlogController.php
@@ -12,7 +12,7 @@ class BlogController extends Controller
/**
* Show a blog entry
*/
- public function showAction($id)
+ public function showAction($id, $slug)
{
$em = $this->getDoctrine()->getEntityManager();
View
65 src/Blogger/BlogBundle/Entity/Blog.php
@@ -25,6 +25,11 @@ class Blog
protected $title;
/**
+ * @ORM\Column(type="string")
+ */
+ protected $slug;
+
+ /**
* @ORM\Column(type="string", length=100)
*/
protected $author;
@@ -90,6 +95,34 @@ public function __toString()
return $this->getTitle();
}
+ public function slugify($text)
+ {
+ // replace non letter or digits by -
+ $text = preg_replace('#[^\\pL\d]+#u', '-', $text);
+
+ // trim
+ $text = trim($text, '-');
+
+ // transliterate
+ if (function_exists('iconv'))
+ {
+ $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
+ }
+
+ // lowercase
+ $text = strtolower($text);
+
+ // remove unwanted characters
+ $text = preg_replace('#[^-\w]+#', '', $text);
+
+ if (empty($text))
+ {
+ return 'n-a';
+ }
+
+ return $text;
+ }
+
/**
* Get id
*
@@ -108,6 +141,8 @@ public function getId()
public function setTitle($title)
{
$this->title = $title;
+
+ $this->setSlug($this->title);
}
/**
@@ -262,4 +297,34 @@ public function getComments()
{
return $this->comments;
}
+
+ /**
+ * Set slug
+ *
+ * @param string $slug
+ */
+ public function setSlug($slug)
+ {
+ $this->slug = $this->slugify($slug);
+ }
+
+ /**
+ * Get slug
+ *
+ * @return string
+ */
+ public function getSlug()
+ {
+ return $this->slug;
+ }
+
+ /**
+ * Add comments
+ *
+ * @param Blogger\BlogBundle\Entity\Comment $comments
+ */
+ public function addComment(\Blogger\BlogBundle\Entity\Comment $comments)
+ {
+ $this->comments[] = $comments;
+ }
}
View
2  src/Blogger/BlogBundle/Resources/config/routing.yml
@@ -17,7 +17,7 @@ BloggerBlogBundle_contact:
_method: GET|POST
BloggerBlogBundle_blog_show:
- pattern: /{id}
+ pattern: /{id}/{slug}
defaults: { _controller: BloggerBlogBundle:Blog:show }
requirements:
_method: GET
View
6 src/Blogger/BlogBundle/Resources/views/Page/index.html.twig
@@ -5,17 +5,17 @@
<article class="blog">
<div class="date"><time datetime="{{ blog.created|date('c') }}">{{ blog.created|date('l, F j, Y') }}</time></div>
<header>
- <h2><a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id }) }}">{{ blog.title }}</a></h2>
+ <h2><a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id, 'slug': blog.slug }) }}">{{ blog.title }}</a></h2>
</header>
<img src="{{ asset(['images/', blog.image]|join) }}" />
<div class="snippet">
<p>{{ blog.blog(500) }}</p>
- <p class="continue"><a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id }) }}">Continue reading...</a></p>
+ <p class="continue"><a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id, 'slug': blog.slug }) }}">Continue reading...</a></p>
</div>
<footer class="meta">
- <p>Comments: <a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id }) }}#comments">{{ blog.comments|length }}</a></p>
+ <p>Comments: <a href="{{ path('BloggerBlogBundle_blog_show', { 'id': blog.id, 'slug': blog.slug }) }}#comments">{{ blog.comments|length }}</a></p>
<p>Posted by <span class="highlight">{{ blog.author }}</span> at {{ blog.created|date('h:iA') }}</p>
<p>Tags: <span class="highlight">{{ blog.tags }}</span></p>
</footer>
View
2  src/Blogger/BlogBundle/Resources/views/Page/sidebar.html.twig
@@ -19,7 +19,7 @@
<article class="comment">
<header>
<p class="small"><span class="highlight">{{ comment.user }}</span> commented on
- <a href="{{ path('BloggerBlogBundle_blog_show', { 'id': comment.blog.id }) }}">
+ <a href="{{ path('BloggerBlogBundle_blog_show', { 'id': comment.blog.id, 'slug': comment.blog.slug }) }}">
{{ comment.blog.title }}
</a>
<em><time datetime="{{ comment.created|date('c') }}">{{ comment.created|created_ago }}</time></em>
Please sign in to comment.
Something went wrong with that request. Please try again.