Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Kohana]dlaczego nie działa przykład z tutoriala
elita4all
post 19.11.2010, 14:40:37
Post #1





Grupa: Zarejestrowani
Postów: 22
Pomógł: 0
Dołączył: 28.10.2009

Ostrzeżenie: (0%)
-----


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

class Controller_Website extends Controller_Template {

    public function __construct(Request $req)
    {
        parent::__construct($req);

        $this->template->links = array
        (
            'Home' => 'home',
            'About' => 'about',
            'Products' => 'products',
            'Contact' => 'contact',
        );
    }

}

ten kod wywala taki błąd:
ErrorException [ Warning ]: Attempt to assign property of non-object
w linijce
Kod
$this->template->links = array



a jak wstawię ten kod do funkcji before to działa
Kod
  public function before()
    {

parent::before();


Ten post edytował elita4all 19.11.2010, 14:47:48
Go to the top of the page
+Quote Post
phpion
post 19.11.2010, 15:09:30
Post #2





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Bo szablon wczytywany jest w metodzie before, a nie w konstruktorze:
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3.  * Abstract controller class for automatic templating.
  4.  *
  5.  * @package Kohana
  6.  * @category Controller
  7.  * @author Kohana Team
  8.  * @copyright (c) 2008-2009 Kohana Team
  9.  * @license <a href="http://kohanaphp.com/license" target="_blank">http://kohanaphp.com/license</a>
  10.  */
  11. abstract class Kohana_Controller_Template extends Controller {
  12.  
  13. /**
  14. * @var string page template
  15. */
  16. public $template = 'template';
  17.  
  18. /**
  19. * @var boolean auto render template
  20. **/
  21. public $auto_render = TRUE;
  22.  
  23. /**
  24. * Loads the template [View] object.
  25. */
  26. public function before()
  27. {
  28. if ($this->auto_render === TRUE)
  29. {
  30. // Load the template
  31. $this->template = View::factory($this->template);
  32. }
  33.  
  34. return parent::before();
  35. }
  36.  
  37. /**
  38. * Assigns the template [View] as the request response.
  39. */
  40. public function after()
  41. {
  42. if ($this->auto_render === TRUE)
  43. {
  44. $this->request->response = $this->template;
  45. }
  46.  
  47. return parent::after();
  48. }
  49.  
  50. } // End Controller_Template

Moja drobna rada: zanim zaczniesz pracę z frameworkiem zapoznaj się z podstawami programowania obiektowego. Bez tej wiedzy będziesz zadawał takie pytania jak to, a komunikat błędu powinien Ci od razu zasugerować, że obiekt szablonu jeszcze nie został stworzony.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 31.07.2025 - 10:52