Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Kohana] Przekazywanie danych do widoku.
Forum PHP.pl > Forum > PHP > Frameworki
Pytajka
Mam taki problem, otóż nie wiem jak do widoku akcji i do headera przekazać zmienne z kontrolera. Widok i akcja są podłączone pod główny widok, przez co nie idzie ich tak przekazać po prostu.

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class Controller_Panel extends Controller {
  4.  
  5. public $template = 'pages/panel';
  6.  
  7. protected $auth;
  8.  
  9. protected $validation;
  10.  
  11. public function before()
  12. {
  13. $this->auth = Auth::instance();
  14.  
  15. $this->template = View::factory( $this->template)->set('header', View::factory('header'));
  16.  
  17. if( $this->auth->logged_in('login') == FALSE){
  18.  
  19. $this->request->redirect('/login');
  20.  
  21. }
  22.  
  23. $this->template->username = $this->auth->get_user()->username;
  24.  
  25.  
  26. }
  27.  
  28. public function action_index()
  29. {
  30. $this->response->body( $this->template->render());
  31.  
  32. }
  33. public function action_logout()
  34. {
  35. if( $this->auth->logout( TRUE)){
  36.  
  37. $this->request->redirect('/');
  38.  
  39. }
  40. }
  41.  
  42. public function action_add()
  43. {
  44. if( isset( $_POST['submit'])){
  45.  
  46. $this->validation = new Validation( $_POST);
  47.  
  48. $this->validation->rule('title', 'not_empty')
  49. ->rule('title', 'min_length', array(':value', 3))
  50. ->rule('title', 'max_length', array(':value', 45))
  51. ->rule('description', 'not_empty')
  52. ->rule('description', 'max_length', array(':value', 255));
  53.  
  54. if( $this->validation->check() == TRUE){
  55.  
  56. // coś tam.
  57.  
  58. }
  59.  
  60. }
  61.  
  62. $this->template->action = View::factory('pages/add');
  63.  
  64. $this->response->body( $this->template->render());
  65. }
  66.  
  67. public function action_edit()
  68. {
  69.  
  70. if( isset( $_POST['submit'])){
  71.  
  72. $this->validation = new Validation( $_POST);
  73.  
  74. $this->validation->rule('city', 'not_empty')
  75. ->rule('city', 'min_length', array(':value', 3))
  76. ->rule('city', 'max_length', array(':value', 55))
  77. ->rule('biography', 'not_empty')
  78. ->rule('biography', 'max_length', array(':value', 255));
  79.  
  80. if( $this->validation->check() == TRUE){
  81.  
  82. $user = $this->auth->get_user();
  83.  
  84. $user->update_user( $_POST, array('city', 'biography'));
  85.  
  86. }
  87.  
  88.  
  89.  
  90. }
  91.  
  92. $this->template->action = View::factory('pages/edit');
  93.  
  94. $this->response->body( $this->template->render());
  95.  
  96. }
  97. }


BlackDante
nagłówki możesz ustalać za pomocą funkcji headers z klasy response np.

  1. $this->response->headers(array('Content-Type' => 'text/html', 'Cache-Control' => 'no-cache'));


A co do przekazywania zmiennych z kontrolera do widoku o ile dobrze myślę nad Twoim problemem to mozesz skorzystać z funkcji bind_global

  1. View::factory('template')->bind_global('var', $var);
szok
Nie lepiej użyć Template_Controller ?
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.