Problemen met action / controller in phalcon framework
Ik heb een controller Users, met een login systeem. Ik heb een view (werkt goed, showt de login form)
en een router die verwijst naar de login pagina.
Codes:
Controller:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Created by PhpStorm.
* User: Yad
* Date: 4-6-2018
* Time: 11:27
*/
use Phalcon\Mvc\Controller;
class UsersController extends Controller
{
public function indexAction(){
$this->view->setTemplateBefore('dtt');
}
public function loginAction() {
session_start();
$sessions = $this->getDI()->getShared("session");
if ($sessions->has("user_id")) {
//if user is already logged we dont need to do anything
// so we redirect them to the main page
return $this->response->redirect("/admin");
}
if ($this->request->isPost()) {
echo "hello world";
$password = $this->request->getPost("password");
$username = $this->request->getPost("username");
if ($username === "") {
$this->flashSession->error("return enter your email");
//pick up the same view to display the flash session errors
return $this->view->pick("login");
}
if ($password === "") {
$this->flashSession->error("return enter your password");
//pick up the same view to display the flash session errors
return $this->view->pick("login");
}
$user = Users::findFirst([
"conditions" => "username = ?0 AND password = ?1",
"bind" == [
0 => $username,
1 => $this->security->hash($password)
]
]);
if (false === $user) {
$this->flashSession->error("wrong user / password");
} else {
$sessions->set("user_id", $user->id);
}
}
}
}
?>
/**
* Created by PhpStorm.
* User: Yad
* Date: 4-6-2018
* Time: 11:27
*/
use Phalcon\Mvc\Controller;
class UsersController extends Controller
{
public function indexAction(){
$this->view->setTemplateBefore('dtt');
}
public function loginAction() {
session_start();
$sessions = $this->getDI()->getShared("session");
if ($sessions->has("user_id")) {
//if user is already logged we dont need to do anything
// so we redirect them to the main page
return $this->response->redirect("/admin");
}
if ($this->request->isPost()) {
echo "hello world";
$password = $this->request->getPost("password");
$username = $this->request->getPost("username");
if ($username === "") {
$this->flashSession->error("return enter your email");
//pick up the same view to display the flash session errors
return $this->view->pick("login");
}
if ($password === "") {
$this->flashSession->error("return enter your password");
//pick up the same view to display the flash session errors
return $this->view->pick("login");
}
$user = Users::findFirst([
"conditions" => "username = ?0 AND password = ?1",
"bind" == [
0 => $username,
1 => $this->security->hash($password)
]
]);
if (false === $user) {
$this->flashSession->error("wrong user / password");
} else {
$sessions->set("user_id", $user->id);
}
}
}
}
?>
Router:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
$router = $di->getRouter();
//route maken voor alle paginas
$router->add(
'/admin/dashboard',
[
'controller' => 'Admin',
'action' => 'dashboard',
]
);
$router->add(
'/admin/login',
[
'controller' => 'Users',
'action' => 'login',
]
);
$router->add(
'/admin/login',
[
'controller' => 'Users',
'action' => 'index',
]
);
$router->add(
'/admin/add-article',
[
'controller' => 'AddArticle',
'action' => '',
]
);
$router->add(
'/articles/archive',
[
'controller' => 'ArticlesArchive',
'action' => '',
]
);
$router->add(
'/admin/edit-article',
[
'controller' => 'EditArticle',
'action' => '',
]
);
$router->add(
'/admin',
[
'controller' => 'Admin',
'action' => 'index',
]
);
$router->add(
'/articles',
[
'controller' => 'Articles',
'action' => '',
]
);
$router->handle();
?>
$router = $di->getRouter();
//route maken voor alle paginas
$router->add(
'/admin/dashboard',
[
'controller' => 'Admin',
'action' => 'dashboard',
]
);
$router->add(
'/admin/login',
[
'controller' => 'Users',
'action' => 'login',
]
);
$router->add(
'/admin/login',
[
'controller' => 'Users',
'action' => 'index',
]
);
$router->add(
'/admin/add-article',
[
'controller' => 'AddArticle',
'action' => '',
]
);
$router->add(
'/articles/archive',
[
'controller' => 'ArticlesArchive',
'action' => '',
]
);
$router->add(
'/admin/edit-article',
[
'controller' => 'EditArticle',
'action' => '',
]
);
$router->add(
'/admin',
[
'controller' => 'Admin',
'action' => 'index',
]
);
$router->add(
'/articles',
[
'controller' => 'Articles',
'action' => '',
]
);
$router->handle();
?>
Views/users/index.phtml:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div class="py-5">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card text-white p-5 bg-primary">
<div class="card-body">
<div class="alert alert-danger" role="alert"> This is a danger alertcheck it out! </div>
<img src="http://localhost/dtt/public/img/dtt.png">
<h1 class="mb-4">Admin login</h1>
<form method="POST">
<div class="form-group">
<label>Username</label>
<input type="username" name="username" class="form-control" placeholder="Username"> </div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Password"> </div>
<button type="submit" name="submit" class="btn btn-secondary btn-lg btn-block">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card text-white p-5 bg-primary">
<div class="card-body">
<div class="alert alert-danger" role="alert"> This is a danger alertcheck it out! </div>
<img src="http://localhost/dtt/public/img/dtt.png">
<h1 class="mb-4">Admin login</h1>
<form method="POST">
<div class="form-group">
<label>Username</label>
<input type="username" name="username" class="form-control" placeholder="Username"> </div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Password"> </div>
<button type="submit" name="submit" class="btn btn-secondary btn-lg btn-block">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Nou is het probleem het volgende:
In de router verwijs ik naar de controller USers, als action login. Dus zodra er een POST vrij komt, hoort hij iets te doen. Maar op deze manier verwijst hij direct door , zonder enkele layout naar de controller en zie ik dus niks, en doet hij niks. Alleen de controller laten zien.
Wat doe ik fout, ligt het aan de controller of de router?
Edit:
Ik heb code-tags geplaatst. Gelieve dit in het vervolg zelf toe te voegen aan je bericht.
Zie ook: Veel gestelde vragen: Welke UBB-codes kan ik gebruiken.
Zie ook: Veel gestelde vragen: Welke UBB-codes kan ik gebruiken.
Gewijzigd op 04/06/2018 19:43:25 door - Ariën -
https://olddocs.phalconphp.com/en/3.0.0/api/Phalcon_Mvc_Router.html zou je van die action=index volgens mij een addGet() (alleen voor GET requests) moeten maken, en van de action=login een addPost() (POST requests = login action).
Je hebt nu 2x een route '/admin/login'. Ik ken "Phalcon" verder niet, maar als ik dit zo lees