Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Kohana]dlaczego nie działa przykład z tutoriala
Forum PHP.pl > Forum > PHP > Frameworki
elita4all
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();
phpion
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.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.