开发者

Kohana 3.2. - How can I use hyphens in URIs

Recently I've been doing some research in开发者_StackOverflow社区to SEO and how URIs that use hyphens or underscores are treated differently, particularly by Google who view hyphens as separators.

Anyway, eager to adapt my current project to meet this criteria I found that because Kohana uses function names to define pages I was receiving the unexpected '-' warning.

I was wondering whether there was any way to enable the use of URIs in Kohana like:

http://www.mysite.com/controller/function-name

Obviously I could setup a routeHandler for this... but if I was to have user generated content, i.e. news. I'd then have to get all articles from the database, produce the URI, and then do the routing for each one.

Are there any alternative solutions?


Note: This is the same approach as in Laurent's answer, just slightly more OOP-wise. Kohana allows one to very easily overload any system class, so we can use it to save us some typing and also to allow for cleaner updates in the future.

We can plug-in into the request flow in Kohana and fix the dashes in the action part of the URL. To do it we will override Request_Client_Internal system class and it's execute_request() method. There we'll check if request->action has dashes, and if so we'll switch them to underscores to allow php to call our method properly.

Step 1. Open your application/bootstrap.php and add this line:

define('URL_WITH_DASHES_ONLY', TRUE);

You use this constant to quickly disable this feature on some requests, if you need underscores in the url.

Step 2. Create a new php file in: application/classes/request/client/internal.php and paste this code:

<?php defined('SYSPATH') or die('No direct script access.');

class Request_Client_Internal extends Kohana_Request_Client_Internal {

    /**
     * We override this method to allow for dashes in the action part of the url
     * (See Kohana_Request_Client_Internal::execute_request() for the details)
     *
     * @param   Request $request
     * @return  Response
     */
    public function execute_request(Request $request)
    {
        // Check the setting for dashes (the one set in bootstrap.php)
        if (defined('URL_WITH_DASHES_ONLY') and URL_WITH_DASHES_ONLY == TRUE) 
        {
            // Block URLs with underscore in the action to avoid duplicated content
            if (strpos($request->action(), '_') !== false)
            {
                throw new HTTP_Exception_404('The requested URL :uri was not found on this server.', array(':uri' => $request->uri()));
            }

            // Modify action part of the request: transform all dashes to underscores
            $request->action( strtr($request->action(), '-', '_') );
        }
        // We are done, let the parent method do the heavy lifting
        return parent::execute_request($request);
    }

} // end_class Request_Client_Internal

What this does is simply replacing all the dashes in the $request->action with underscores, thus if url was /something/foo-bar, Kohana will now happily route it to our action_foo_bar() method.

In the same time we block all the actions with underscores, to avoid the duplicated content problems.


No way to directly map a hyphenated string to a PHP function so you will have to do routing.

As far as user generated content, you could do something like Stack Exchange does. Each time user content is saved to the database, generated a slug for it (kohana-3-2-how-can-i-use-hyphens-in-uris) and save it along with the other information. Then when you need to link to it, use the unique id and append the slug to the end (ex:http://stackoverflow.com/questions/7404646/kohana-3-2-how-can-i-use-hyphens-in-uris) for readability.


You can do this with lambda functions: http://forum.kohanaframework.org/discussion/comment/62581#Comment_62581


You could do something like

Route::set('route', '<controller>/<identifier>', array(
    'identifier' => '[a-zA-Z\-]*'
))
->defaults(array(
    'controller' => 'Controller',
    'action'     => 'show',
));

Then receive your content identifier in the function with Request::current()->param('identifier') and parse it manually to find the relating data.


After having tried various solutions, I found that the easiest and most reliable way is to override Kohana_Request_Client_Internal::execute_request. To do so, add a file in your application folder in "application\classes\kohana\request\client\internal.php" then set its content to:

<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_Request_Client_Internal extends Request_Client {

    /**
     * @var    array
     */
    protected $_previous_environment;

    /**
     * Processes the request, executing the controller action that handles this
     * request, determined by the [Route].
     *
     * 1. Before the controller action is called, the [Controller::before] method
     * will be called.
     * 2. Next the controller action will be called.
     * 3. After the controller action is called, the [Controller::after] method
     * will be called.
     *
     * By default, the output from the controller is captured and returned, and
     * no headers are sent.
     *
     *     $request->execute();
     *
     * @param   Request $request
     * @return  Response
     * @throws  Kohana_Exception
     * @uses    [Kohana::$profiling]
     * @uses    [Profiler]
     * @deprecated passing $params to controller methods deprecated since version 3.1
     *             will be removed in 3.2
     */
    public function execute_request(Request $request)
    {
        // Create the class prefix
        $prefix = 'controller_';

        // Directory
        $directory = $request->directory();

        // Controller
        $controller = $request->controller();

        if ($directory)
        {
            // Add the directory name to the class prefix
            $prefix .= str_replace(array('\\', '/'), '_', trim($directory, '/')).'_';
        }

        if (Kohana::$profiling)
        {
            // Set the benchmark name
            $benchmark = '"'.$request->uri().'"';

            if ($request !== Request::$initial AND Request::$current)
            {
                // Add the parent request uri
                $benchmark .= ' « "'.Request::$current->uri().'"';
            }

            // Start benchmarking
            $benchmark = Profiler::start('Requests', $benchmark);
        }

        // Store the currently active request
        $previous = Request::$current;

        // Change the current request to this request
        Request::$current = $request;

        // Is this the initial request
        $initial_request = ($request === Request::$initial);

        try
        {
            if ( ! class_exists($prefix.$controller))
            {
                throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
                                                    array(':uri' => $request->uri()));
            }

            // Load the controller using reflection
            $class = new ReflectionClass($prefix.$controller);

            if ($class->isAbstract())
            {
                throw new Kohana_Exception('Cannot create instances of abstract :controller',
                    array(':controller' => $prefix.$controller));
            }

            // Create a new instance of the controller
            $controller = $class->newInstance($request, $request->response() ? $request->response() : $request->create_response());

            $class->getMethod('before')->invoke($controller);

            // Determine the action to use
            /* ADDED */ if (strpos($request->action(), '_') !== false) throw new HTTP_Exception_404('The requested URL :uri was not found on this server.', array(':uri' => $request->uri()));
            /* MODIFIED */ $action = str_replace('-', '_', $request->action()); /* ORIGINAL: $action = $request->action(); */

            $params = $request->param();

            // If the action doesn't exist, it's a 404
            if ( ! $class->hasMethod('action_'.$action))
            {
                throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
                                                    array(':uri' => $request->uri()));
            }

            $method = $class->getMethod('action_'.$action);

            $method->invoke($controller);

            // Execute the "after action" method
            $class->getMethod('after')->invoke($controller);
        }
        catch (Exception $e)
        {
            // Restore the previous request
            if ($previous instanceof Request)
            {
                Request::$current = $previous;
            }

            if (isset($benchmark))
            {
                // Delete the benchmark, it is invalid
                Profiler::delete($benchmark);
            }

            // Re-throw the exception
            throw $e;
        }

        // Restore the previous request
        Request::$current = $previous;

        if (isset($benchmark))
        {
            // Stop the benchmark
            Profiler::stop($benchmark);
        }

        // Return the response
        return $request->response();
    }
} // End Kohana_Request_Client_Internal

Then to add an action with hyphens, for example, "controller/my-action", create an action called "my_action()".

This method will also throw an error if the user tries to access "controller/my_action" (to avoid duplicate content).

I know some developers don't like this method but the advantage of it is that it doesn't rename the action, so if you check the current action it will be consistently called "my-action" everywhere. With the Route or lambda function method, the action will sometime be called "my_action", sometime "my-action" (since both methods rename the action).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜