How to send 410 departed to cakephp 2.x

I want the server to send a 410 response for my old unpublished urls in CakePHP.

CakePHP provides a lot of exceptions like 404, 500, etc., but nothing like it remains.

Is there a way to send the server 410 a response if someone opens an old url that no longer exists on the website but was there before.

Thank,

+3


source to share


2 answers


I did the following to solve this.

I added the "gone" function to "AppExceptionRenderer"

public function gone($error) {

        $this->controller->layout = 'nopageexists';

        $this->controller->set('title_for_layout', __('example.com: Page not found'));
        $this->controller->set('meta_description', '' );
        $this->controller->set('meta_keywords', '');

        // This will execute 404 Error Page without redirection added by vinod...
        $this->controller->response->statusCode(410);
        $this->controller->render('/Errors/error404');
        $this->controller->response->send();
    }

      

Created a custom class at app / Lib / myException.php



class GoneException extends CakeException {

/**
 * Constructor
 *
 * @param string $message If no message is given 'Not Found' will be the message
 * @param integer $code Status code, defaults to 410
  */
    /*
    public function __construct($message = null, $code = 410) {
        if (empty($message)) {
            $message = 'Gone';
        }
        parent::__construct($message, $code);
    }
    */
    protected $_messageTemplate = 'Test';
}

      

Added below in the controller where I want to show 410 left.

throw new GoneException('Gone', 410);

      

This worked for me.

0


source


The preferred way is exclusion. Exceptions propagated to HttpException. You can also go from this class and create your own custom exception.

https://github.com/cakephp/cakephp/blob/3.0.11/src/Network/Exception/BadRequestException.php



<?php

use Cake\Network\Exception\HttpException;

class GoneException extends HttpException
{
    /**
     * Constructor
     *
     * @param string $message If no message is given 'Gone' will be the message
     * @param int $code Status code, defaults to 410
     */
    public function __construct($message = null, $code = 410)
    {
        if (empty($message)) {
            $message = 'Gone';
        }
        parent::__construct($message, $code);
    }
}

      

+2


source







All Articles