0

Hi so how do you do it in kohana 3.3 and kostache?

Form

<form method="POST" action="user/login">

<input type="text" name="email" />
<input type="passowrd" name="password" />

</form>

Controller

 public function action_login()
 {
   $user = Auth::instance()->login($this->request->post('email'),$this->request->post('password'));

   if($user)
   {
       $view = Kostache_Layout::factory()
       $layout = new View_Pages_User_Info();

       $this->response->body($this->view->render($layout));
   }
   else
   {
       $this->show_error_page();
   }

 }

Class View

class View_Pages_User_Info
{
    public $title= "Profile";
}

Mustache Template

   <p> This is the Profile Page</p>

So far so good, I'm now in the profile page but the url is

localhost/kohana_app/user/login 

instead of

localhost/kohana_app/user/profile

I know I can change the action_login to action_profile to match the url and the title of the page but is there any other way to do this?

Harish Singh
  • 3,359
  • 5
  • 24
  • 39
Defyleiti
  • 535
  • 1
  • 7
  • 23

1 Answers1

1

Forget about a body for the response if the login was succesful and redirect to the profile page.

HTTP::redirect(Route::get('route that routes to the profile page')->uri(/* Just guessing */'action' => 'profile'));

Read up on Post/Redirect/Get.


Example route(s) as requested

Route::set('home', '')
    ->defaults(array(
        'controller' => 'Home',
    ));

Route::set('auth', 'user/<action>', array('action' => 'login|logout'))
    ->defaults(array(
        'controller' => 'User',
    ));

Route::set('user/profile/edit', 'user/profile/edit(/<user>)')
    ->defaults(array(
        'controller' => 'User_Profile', // Controller_User_Profile
        'action' => 'edit',
    ));

Route::set('user/profile/view', 'user/profile(/<action>(/<user>))', array('action' => 'edit'))
    ->defaults(array(
        'controller' => 'User_Profile',
    ));

############

class Controller_User_Profile {

    public function action_index()
    {
        // ...

        $this->action_view($user->username());
    }

    public function action_view($user = NULL)
    {
        if ($user === NULL)
        {
            $user = $this->request->param('user');
        }

        // ...
    }
}

Personally I like to send my users to a dashboard, which can be(come) different from viewing your own profile.

This is just A way of doing it.

Darsstar
  • 1,885
  • 1
  • 14
  • 14
  • So for this i'll need to set another route in the bootstrap file?.. Is that right then use it on my profile page. – Defyleiti Dec 12 '13 at 07:30
  • http://kohanaframework.org/3.3/guide/kohana/tips#dont-try-and-use-one-route-for-everything Best thing you can do is to delete the 'default' example route and create more specific routes. Catch all routes are bad. So avoid them if you can, it looks like you are starting from scratch, so you have no excuse :p Again: DELETE the 'default' route. – Darsstar Dec 12 '13 at 07:47