Changer automatiquement le title en fonction de l’action

Comme j’oublie tout le temps de définir la balise Title spécifique à chaque page je me suis dis que mon ami symfony pourrai peut être penser à ma place, voici donc mon code

Dans lib/filter/sfAutoMetaFilter.class.php

<?php
/**
 * Manage MetaTags 
 * @author Cédric Lombardot 
 */
 
class sfAutoMetaFilter extends sfFilter{
    public function execute ($filterChain)
      {
        $filterChain->execute();
        /*
         * Recherche le h1 pour mettre le title
         */
 
 
        $html=$this->getContext()->getResponse()->getContent(); 
        $title=$this->get_title($html);
        $h1s=$this->get_h1($html);
        if($h1s[1]==0){
            $h2s=$this->get_h2($html);
            if($h2s[1]!=0){
                $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h2s[0][0])));
            }
        }else{
            $this->getContext()->getResponse()->setContent($this->set_title($html,(($title)?($title.' - ' ):'').strip_tags($h1s[0][0])));
        }
 
 
 
 
      }
 
      // retrieve all h1 tags
        protected function get_h1($html){
            $h1tags = preg_match_all("/()(.*)(<\/h1>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_h2($html){
            $h2tags = preg_match_all("/()(.*)(<\/h2>)/isxmU",$html,$patterns);
            $res = array();
            array_push($res,$patterns[2]);
            array_push($res,count($patterns[2]));
            return $res;
        } 
        protected function get_title($html){
            $title = preg_match("/()(.*)(<\/title>)/isxmU",$html,$patterns);
            return isset($patterns[2])?$patterns[2]:'';
        } 
        protected function set_title($html,$title){
            return preg_replace("/(',$html);
        } 
}
?>
</pre>
<p>Et dans le filters.yml</p>
<pre name="code" class="php">autometa:
  class: sfAutoMetaFilter
</pre>
					</div><!-- .entry-content -->
		
		<footer class="entry-meta">
			Cette entrée a été publiée dans <a href="http://blog.lombardot.fr/category/symfony/" title="Voir tous les articles dans Symfony" rel="category tag">Symfony</a>, et marquée avec <a href="http://blog.lombardot.fr/tag/filter/" rel="tag">filter</a>, <a href="http://blog.lombardot.fr/tag/symfony/" rel="tag">Symfony</a>, le <a href="http://blog.lombardot.fr/2010/05/05/changer-automatiquement-le-title/" title="3 h 27 min" rel="bookmark"><time class="entry-date" datetime="2010-05-05T03:27:53+00:00">5 mai 2010</time></a> <span class="by-author">par <span class="author vcard"><a class="url fn n" href="http://blog.lombardot.fr/author/admin/" title="Afficher tous les articles par Cédric Lombardot" rel="author">Cédric Lombardot</a></span></span>.								</footer><!-- .entry-meta -->
	</article><!-- #post -->

				<nav class="nav-single">
					<h3 class="assistive-text">Navigation des articles</h3>
					<span class="nav-previous"><a href="http://blog.lombardot.fr/2010/04/14/savoir-si-un-widget-est-requis/" rel="prev"><span class="meta-nav">←</span> Savoir si un widget est requis</a></span>
					<span class="nav-next"><a href="http://blog.lombardot.fr/2010/05/21/debuggez-vos-requettes-sql-propel/" rel="next">Débuggez vos requêttes SQL / Propel <span class="meta-nav">→</span></a></span>
				</nav><!-- .nav-single -->

				
<div id="comments" class="comments-area">

	
	
									<div id="respond" class="comment-respond">
				<h3 id="reply-title" class="comment-reply-title">Laisser un commentaire <small><a rel="nofollow" id="cancel-comment-reply-link" href="/2010/05/05/changer-automatiquement-le-title/#respond" style="display:none;">Annuler la réponse.</a></small></h3>
									<p class="must-log-in">Vous devez <a href="http://blog.lombardot.fr/wp-login.php?redirect_to=http%3A%2F%2Fblog.lombardot.fr%2F2010%2F05%2F05%2Fchanger-automatiquement-le-title%2F">être connecté</a> pour rédiger un commentaire.</p>												</div><!-- #respond -->
			
</div><!-- #comments .comments-area -->
			
		</div><!-- #content -->
	</div><!-- #primary -->


			<div id="secondary" class="widget-area" role="complementary">
			<aside id="categories-3" class="widget widget_categories"><h3 class="widget-title">Catégories</h3><select name='cat' id='cat' class='postform' >
	<option value='-1'>Choisir une catégorie</option>
	<option class="level-0" value="11">Divers  (7)</option>
	<option class="level-1" value="12">   Shell  (7)</option>
	<option class="level-0" value="10">JQuery  (1)</option>
	<option class="level-0" value="1">Non classé  (4)</option>
	<option class="level-0" value="3">Symfony  (10)</option>
	<option class="level-1" value="8">   Doctrine  (1)</option>
	<option class="level-1" value="6">   Form  (1)</option>
	<option class="level-1" value="4">   Propel  (3)</option>
	<option class="level-1" value="9">   widget  (1)</option>
</select>

<script type='text/javascript'>
/* <![CDATA[ */
	var dropdown = document.getElementById("cat");
	function onCatChange() {
		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
			location.href = "http://blog.lombardot.fr/?cat="+dropdown.options[dropdown.selectedIndex].value;
		}
	}
	dropdown.onchange = onCatChange;
/* ]]> */
</script>

</aside><aside id="rss-3" class="widget widget_rss"><h3 class="widget-title"><a class='rsswidget' href='http://www.symfony-project.org/plugins/recently.rss' title='Suivez ce contenu par syndication RSS'><img style='border:0' width='14' height='14' src='http://blog.lombardot.fr/wp-includes/images/rss.png' alt='RSS' /></a> <a class='rsswidget' href='http://www.symfony-project.org/plugins/' title='symfony Project Recently updated Plugins'>Les plugins symfony</a></h3><ul><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineCronManagerPlugin/1_0_0' title='Changelog for release              1.0.0             published on 21/10/2013                                                  Release of stable code                                                                                                                                Description                                                         Allows users to administer cron jobs from a backend interface. Useful for users with limited admin access to the server. Jobs can be added, deleted, or run at any time for testing or other purposes.                                                                          '>sfDoctrineCronManagerPlugin 1.0.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfCacheTaggingPlugin/4_4_0' title='Changelog for release              4.4.0             published on 30/12/2012                                                  [Handling cache tags rollback/commit inside Doctrine transactions GH-18 [added] New two app.yml settings to customize collection and object tag names [updated] API partially changed [updated] README [fixed] Along the way fixed more than one minor bug and IMHO one major bug [fixed] Almost all tests were fixed because they used Doctrine transaction to keep test fixtures unchanged                                                                                                                                Description                                                         The sfCacheTaggingPlugin is a Symfony plugin that allows you to not think about     cache obsolescence. The user will see only a fresh data thanks to cache tagging.     The plugin helps to keep cached content up-to-date without setting cache lifetime.     So-called "Smart caching", stores cache with fetched models tags and its versions.     Each time you save/update/delete objects, plugin increments object tag version and     all linked cache by specific tag name will automatically spoil.                                                                          '>sfCacheTaggingPlugin 4.4.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfCacheTaggingPlugin/4_3_0' title='Changelog for release              4.3.0             published on 16/12/2012                                                  [fixed] When SoftDelete installed after Cachetaggable (actAs section), preDqlDelete forces to update already softly deleted records (thanks to Paul Moore) [added] New approach to append custom parameter(-s) to the cache key only for authenticated users [added] more coverage tests                                                                                                                                Description                                                         The sfCacheTaggingPlugin is a Symfony plugin that allows you to not think about     cache obsolescence. The user will see only a fresh data thanks to cache tagging.     The plugin helps to keep cached content up-to-date without setting cache lifetime.     So-called "Smart caching", stores cache with fetched models tags and its versions.     Each time you save/update/delete objects, plugin increments object tag version and     all linked cache by specific tag name will automatically spoil.                                                                          '>sfCacheTaggingPlugin 4.3.0</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineApplyPlugin/1_2_2' title='Changelog for release              1.2.2             published on 13/11/2012                                                  License reflects P'unk Avenue's sponsorship of this plugin                                                                                                                                Description                                                         Allows users of a site protected by sfDoctrineGuardPlugin to create new accounts, verifying them by email. Both account creation and password changes require email verification as a speed bump to slow down spammers.                                                                          '>sfDoctrineApplyPlugin 1.2.2</a></li><li><a class='rsswidget' href='http://www.symfony-project.org/plugins/sfDoctrineApplyPlugin/1_2_1' title='Changelog for release              1.2.1             published on 13/11/2012                                                  Version 1.2 (which was only released via svn) added support for the newer stable version of sfDoctrineGuardPlugin and has been the only one we've used for some time. Uses SwiftMailer like other sensible Symfony 1.4 projects.   Version 1.2.1 of sfDoctrineApplyPlugin optionally supports Facebook login. Also refactored the partials a bit to make it easier to selectively override still with all the new Facebook stuff present. Facebook logins capture email addresses and full names. More 1.2-era changes: all forms are now empty extensions of Base versions, so you can do the usual Symfony thing to override them at the project level (specifying a particular replacement class via app.yml is still supported too). Made private methods in BasesfApplyActions protected. Removed setting of form formatter in forms.                                                                                                                                 Description                                                         Allows users of a site protected by sfDoctrineGuardPlugin to create new accounts, verifying them by email. Both account creation and password changes require email verification as a speed bump to slow down spammers.                                                                          '>sfDoctrineApplyPlugin 1.2.1</a></li></ul></aside><aside id="text-4" class="widget widget_text"><h3 class="widget-title">Mon twitter</h3>			<div class="textwidget"><div id="twitter_div" class="linkcat">
<ul id="twitter_update_list"></ul>
<a href="http://twitter.com/cedriclombardot" id="twitter-link" style="display:block;text-align:right;">Retrouvez moi sur Twitter</a>
</div>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/cedriclombardot.json?callback=twitterCallback2&count=5"></script></div>
		</aside><aside id="text-5" class="widget widget_text"><h3 class="widget-title">Contactez-moi</h3>			<div class="textwidget">  <img src="http://0.gravatar.com/avatar/e33e3b0f5cd521f7f9e0ee1096c3d0ed?s=70&d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D30&r=G" style="float:right"/>  <b>Cédric Lombardot</b><br>
        44 Avenue de saint Ouen<br>
        75 018 PARIS<br>
         
          <ul class="contact">
            <li> <img height="16" width="16" alt="phone" src="http://lombardot.fr/squelettes/images/phone.png"> : 06 01 91 57 40 </li>
            <li> <img alt="email" src="http://lombardot.fr/squelettes/images/email.png"> : <a href="http://lombardot.fr/spip.php?page=contact">cedric.lombardot@gmail.com</a> </li>
            <li> <img alt="viadeo" src="http://lombardot.fr/squelettes/images/viadeo-logo.png"> : <a target="_blank" href="http://www.viadeo.com/fr/profile/cedric.lombardot">Mon Profil viadeo</a> </li>
          	<li> <img alt="blog" src="http://lombardot.fr/squelettes/images/kblogger.png"> : <a target="_blank" href="http://cedric.lombardot.fr">Mon CV</a> </li>

<li> <img alt="blog" src="http://lombardot.fr/squelettes/images/kblogger.png"> : <a target="_blank" href="http://symfony2admingenerator.org">Admingenerator symfony2</a> </li>
          </ul>

</div>
		</aside><aside id="text-6" class="widget widget_text">			<div class="textwidget"><script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-4500211-3");
pageTracker._initData();
pageTracker._trackPageview();
</script>
<style>
#text-6{ display:none }
</style>
</div>
		</aside>		</div><!-- #secondary -->
		</div><!-- #main .wrapper -->
	<footer id="colophon" role="contentinfo">
		<div class="site-info">
						<a href="http://wordpress.org/" title="Plate-forme de publication personnelle à la pointe de la sémantique">Fièrement propulsé par WordPress</a>
		</div><!-- .site-info -->
	</footer><!-- #colophon -->
</div><!-- #page -->

<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shCore.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCSharp.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushPhp.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushJScript.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushJava.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushVb.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushSql.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushXml.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushDelphi.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushPython.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushRuby.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCss.js"></script>
<script class="javascript" src="http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/shBrushCpp.js"></script>
<script class="javascript">
dp.SyntaxHighlighter.ClipboardSwf = 'http://blog.lombardot.fr/wp-content/plugins/google-syntax-highlighter/Scripts/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<script type='text/javascript' src='http://blog.lombardot.fr/wp-content/themes/twentytwelve/js/navigation.js?ver=1.0'></script>
</body>
</html>