Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [ZF2][PHP] Wywołanie to route\ to url poza używanym kontrolerem
Geniesis
post 7.04.2015, 18:10:13
Post #1





Grupa: Zarejestrowani
Postów: 53
Pomógł: 5
Dołączył: 4.12.2013
Skąd: Śląsk

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


Witam.
Na początek trochę kodu (przykładowe użycie):

CreatorController (zf2.localhost/creator/index)
  1. <?php
  2. namespace Creator\Controller;
  3. use Zend\View\Model\ViewModel;
  4. use Zend\Mvc\Controller\AbstractActionController;
  5. use Creator\Controller\SessionServiceController;
  6.  
  7. class CreatorController extends AbstractActionController{
  8.  
  9. public function __construct(){
  10. $this -> sessionService = new SessionServiceController();
  11. }
  12.  
  13. public function indexAction(){
  14. if($this-> sessionService ->checkSession()){
  15. echo 'Witam';
  16. }
  17. }
  18. }

SessionServiceController:
  1. namespace Creator\controller;
  2. use Zend\Mvc\Controller\AbstractActionController;
  3. use Zend\Session\Container;
  4. use Creator\Controller\SessionServiceController;
  5.  
  6. class SessionServiceController extends AbstractActionController {
  7.  
  8. const CONTAINER_SESSION_ID = 'usr_id';
  9.  
  10. public function __construct(){
  11. $this -> session_id = new Container( self::CONTAINER_SESSION_ID);
  12. }
  13.  
  14. public function checkSession(){
  15. if (empty($this ->session_id->usr_id)){
  16. //$this->redirect()->toUrl("zf2.localhost/authUser/index");
  17. return $this->redirect()->toRoute('creator/default',
  18. array('controller'=>'authUser', 'action'=>'index'));
  19. }
  20.  
  21. if (!empty($this ->session_id->usr_id)){
  22. return true;
  23. }
  24. }
  25.  
  26. }


Co chcę osiągnąć:
Chcę by po spełnieniu warunku w funkcji checkSession przekierowało mnie do zf2.localhost/authUser/index
całość działa gdy bezpośrednio z CreatorController wykonuję toRoute, jednak w sytuacji kilku plików, nie ma sensu powtarzać kilkukrotnie tej samej funkcji, stąd chcę ją mieć w osobnym pliku i tylko się do niej odwoływać (tak jak wyżej).

Dlatego proszę o pomoc w wytłumaczeniu co robię źle,
że wyświetla mi błąd: Url plugin requires that controller event compose a router; none found

Jeżeli jest lepszy sposób na poradzenie sobie z tym to także proszę o nakierowanie.

Pozdrawiam.
Go to the top of the page
+Quote Post
kilaz91
post 29.04.2015, 09:34:51
Post #2





Grupa: Zarejestrowani
Postów: 14
Pomógł: 0
Dołączył: 3.04.2014

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


Może coś takiego:
Controller:

public function indexAction(){
if( ! $this-> sessionService ->checkSession() )
return $this-> sessionService ->checkSession();
}

SessionService:
public function checkSession(){
if (empty($this ->session_id->usr_id)){
//$this->redirect()->toUrl("zf2.localhost/authUser/index");
return $this->redirect()->toRoute('creator/default',
array('controller'=>'authUser', 'action'=>'index'));
}

if (!empty($this ->session_id->usr_id)){
return false;
}
}

Za każdym razem zwraca Ci wartość TRUE. W warunku niepusta zmienna jest wartością TRUE smile.gif

W większości przypadków, u mnie sprawdza się Application/Module.php i onBootstrap a w nim:

$app->getEventManager()->attach(
'route',
function($e) {
$app = $e->getApplication();
$routeMatch = $e->getRouteMatch();
$sm = $app->getServiceManager();

$this->route=$routeMatch->getMatchedRouteName();

//strony wymagające autoryzacji
if($this->isPageWithAuth()){
$this->setCss($sm,['admin']);

//jeżeli nie jest zalogowany, przenieś do strony logowania
if (!$this->UserSession()->isLogged()) {

$this->UserSession()->setRedirectReferer($this->route,$routeMatch->getParams());
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine(
'Location',
$e->getRouter()->assemble(
array(),
array('name' => 'auth/login')
)
);
$response->setStatusCode(302);
return $response;
}

gdzie w isPageWithAuth()


private function isPageWithAuth(){

if($this->route=='auth/login' || $this->route=='auth/register' || $this->route == 'auth/forgot'){
return false;
}

$routers=explode('/',$this->route);
if(isset($routers[0])){
$route=$routers[0];
if($route=='auth' || $route=='admin' || $route=='superadmin' || $route=='webmaster'){
return true;
}
}

return false;
}


smile.gif
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: 13.06.2025 - 07:42