Loading profile only with parameter without the need to key in Kohana 3.x

StackOverflow https://stackoverflow.com/questions/18303106

  •  24-06-2022
  •  | 
  •  

Вопрос

I'm new to Kohana 3.x. Would you like a website with Kohana with User profile style twitter. Example: https://twitter.com/maronems to load the profile is passed only paramentro maronems without the need to pass the key = parameter. Please can someone help me?

Это было полезно?

Решение

By "key = parameter" I assume you mean something like http://twitter.com?user=maronems right? This is ugly, we can do better.

Let's look at making your URLs look like http://twitter.com/maronems instead.

You'll want to look at Kohana's routing system.

Take a look at this route:

Route::set('username route', '<username>')
->defaults(array(
    'controller' => 'Profile',
    'action'     => 'index', 
));
  • Firstly, it's called username route, this is an aribitrary name, but a good one because it's intent is clear.

  • Next look at the regex pattern <username>. This route is going to capture the username and store it in a variable called username.

  • Now notice that the route doesn't have to specify the controller and action. The routing system will get those from the default values. In this example you'll need a controller called Controller_Profile with an action called action_index.

So let's look at the controller now:

<?php 

class Controller_Profile extends Controller {

    function action_index()
    {
        echo 'Hello ' . $this->request->param('username');
    }

}

Of course you shouldn't user echo like this in classes, but to illustrate the point, if you visit example.com/maronems you should see Hello maronems echoed out.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top