rss
twitter
  • Showcase
  • Carrières
  • Support
  • GitLab
  • Espace client
  • Contact

Handle 404 errors properly (WordPress-like) with Kohanaphp Framework

0

INDEX

Oui j’ai mis le titre en anglais car je compte beaucoup sur le SEO de cet article, gniarf.

Based on this article at Rapidop

Goal of the application

– You create a CMS where you can register Pages
– Each page has a slug URL, i.e. : « nananana.com/slug » <— as in WordPress « pages »
– You want Kohana 2.x to load an article based on the slug
– So you need to catch the 404 (beware not to name your slugs as real Controllers names !!)
– And you need to redirect to the Main Controller and use the __call() method.

Go !

Config : enabling hooks

in /application/config/config.php :

/**
 * Enable or disable hooks.
 */
$config['enable_hooks'] = TRUE;

Create a hook file named my_errors in /application/hooks folder.Retour ligne automatique
In /application/hooks/my_errors.php :

<?php

defined('SYSPATH') or die('No direct script access.');
Event::clear('system.404', array('Kohana', 'show_404'));
Event::add('system.404', 'my_404');

function my_404() {
    // get the first part of the requested URI
    $segment = Router::$current_uri;
    url::redirect('main/'.$segment);
}

?>

Checking the Main Controller

– First things first, you can use any Controller name you need. Mine is called « main ».
–  Check that you have a __call() methode inside, like this :

public function __call($method, $arguments) {
        // Disable auto-rendering
        $this->auto_render = FALSE;

        // By defining a __call method, all pages routed to this controller
        // that result in 404 errors will be handled by this method, instead of
        // being displayed as "Page Not Found" errors.
        echo 'This text is generated by __call. If you expected the index page, you need to use: welcome/index/' . substr(Router::$current_uri, 8);
    }

At this step if you go to, for example, https://localhost/main/polarity-therapy you should see this error message generated by Kohana :
This text is generated by __call. If you expected the index page, you need to use : welcome/index/arity-therapy

Updating the __call() method

public function __call($method, $arguments) {
        // the page we're trying to reach == the method kohana tries to load
        $page = Router::$method;

        // is there such a slug in the pages?
        $temp = ORM_MPTT::factory('pages')->where('slug', $page)->find_all();
        if (count($temp)) {
            // in the Page Controller you deploy a normal page serving system
            url::redirect('main/page/' . $page);
        } else {
            // the slug doesn't exist, so we need to throw a real 404 page this time.
            $this->template->texte = new View('404');
        }
    }

Comments are explicit so I leave this block to your appreciation.

It works like this. Enjoy !

DATE 29 Nov 2011
by : Germain
Author / Auteur

Social Share / Partager

    Leave a Reply / Répondre Annuler la réponse

    *
    *

    More in Back-end : PHP, CMS

    • Quel php.ini (MAMP ou Linux), quelle valeur de phpinfo() ?
    • Trucs utiles pour WordPress
    • Les bons headers PHP pour envoyer du CSV compatible Excel en UTF8
    • Imagemanager(tiny Mce) et kohana
    • Récupérer, vérifier et exploiter une date

    Search the blog

    Blog categories

    • Workflow (1)
    • Front-end : HTML, CSS (13)
    • Front-end : javascript, jQuery (33)
    • Back-end : PHP, CMS (42)
    • Back-end : SQL, MySQL (20)
    • Sysadmin : Linux, Apache, GIT (75)
    • Designers : tools, advice (2)
    • Desktop : OS X (37)
    • Desktop : Microsoft (12)
    • Uncategorized (8)

    ITALIC™ Resources

    • Paramètres de connexion au serveur mail
    • Graphistes : séduire un développeur web
    • Dév front : nos bonnes pratiques
    • Mailing : nos (bonnes ?) pratiques
    • Environnement de travail Mac

    Recent posts

    • Autoriser le chargement de contenus mixtes non-https dans Chrome (Mac)
    • Afficher les logs MySQL
    • Recevoir un mail lors de la connexion root à un serveur
    • Changer l’interclassement de plusieurs tables MySQL à la fois (ou presque
    • Maildev : tester l’envoi de mails durant ses développements
    © 2008-2017 ITALIC™ • 8 bis rue Abel • 75012 PARIS • Tel +33 (0)1 48 44 46 35 • RCS PARIS 508 228 772 • Powered by WordPress & GoodLayers
    Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site web.
    Cookie settingsACCEPTER
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Toujours activé
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    Enregistrer & appliquer