Nette nenajde třídu Users
- Zuben45
- Člen | 268
Dobrý den,
vytvářím si registraci, vytvořil jsem model Users.php a
RegisterPresenter.php, ale stránka mi stále hlásí:
Class Users used in service 'users' has not been found or is not instantiable.
Config
users:
class: Users(@dibi.connection)
Model/Users.php
<?
use Nette;
class Users extends \Nette\Object {
/** @var \DibiConnection */
private $db;
private $table = "users";
public static $user_salt = "AEcx199opQ";
public function __construct(\DibiConnection $connection) {
$this->db = $connection;
}
public function findAll() {
return $this->db->select('*')->from($this->table);
}
public function register($data) {
unset($data["password2"]);
$data["role"] = "guest";
$data["password"] = sha1($data["password"] . self::$user_salt);
return $this->db->insert($this->table, $data)->execute(dibi::IDENTIFIER);
}
}
RegisterPresenter.php
<?php
use Nette\Application\UI,
Nette\Application\UI\Form as Form;
class RegisterPresenter extends BasePresenter {
/** @var Users */
private $users;
protected function startup() {
parent::startup();
$this->users = $this->context->users;
}
public function renderRegister(){
}
protected function createComponentRegisterForm() {
$form = new Form;
$form->addText('name', 'Jméno');
$form->addText('email', 'E-mail: *', 35)
->setEmptyValue('@')
->addRule(Form::FILLED, 'Vyplňte Váš email')
->addCondition(Form::FILLED)
->addRule(Form::EMAIL, 'Neplatná emailová adresa');
$form->addPassword('password', 'Heslo: *', 20)
->setOption('description', 'Alespoň 6 znaků')
->addRule(Form::FILLED, 'Vyplňte Vaše heslo')
->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaků.', 6);
$form->addPassword('password2', 'Heslo znovu: *', 20)
->addConditionOn($form['password'], Form::VALID)
->addRule(Form::FILLED, 'Heslo znovu')
->addRule(Form::EQUAL, 'Hesla se neshodují.', $form['password']);
$form->addSubmit('send', 'Registrovat');
$form->onSuccess[] = callback($this, 'registerFormSubmitted');
return $form;
}
public function registerFormSubmitted(UI\Form $form) {
$values = $form->getValues();
$new_user_id = $this->users->register($values);
if($new_user_id){
$this->flashMessage('Registrace se zdařila, jo!');
$this->redirect('Sign:in');
}
}
}
Register/register.php
{block #content}
<h2>Registrovat</h2>
{control registerForm}
Nevíte proč se mi nechce načíst třída Users ? Děkuji mockrát
- Michal Vyšinský
- Člen | 608
Máš ty svoje třídy někde, kam může robotloader? Jestli ano, zkus smazat obsah složky temp/cache
- Zuben45
- Člen | 268
koudis napsal(a):
Mam uplne stejny problem. Hraju si stim uz skoro dve hodinky.
Dam ti jednu dobrou radu – chces-li si zachovat zdravy rozum, neprogramuj v PHP. Vyber si neco, co neni jeden velky balast :)…
mě baví ale PHP :), teď něco zkouším, jestli se to povede, určo napíšu jak jsem to vyřešil ;)
- Zuben45
- Člen | 268
Vyřešeno ;)
koudis napsal(a):
Mam uplne stejny problem. Hraju si stim uz skoro dve hodinky.
Dam ti jednu dobrou radu – chces-li si zachovat zdravy rozum, neprogramuj v PHP. Vyber si neco, co neni jeden velky balast :)…
tady to máš:
Config
services:
usersRepository: Todo\UsersRepository
UsersRepository.php
<?php
namespace Todo;
use Nette;
/**
* Tabulka user
*/
class UsersRepository extends Repository
{
public static $user_salt = "AEcx199opQ";
public function register($data) {
unset($data["password2"]);
$data["role"] = "guest";
$data["password"] = sha1($data["password"] . self::$user_salt);
$toReturn = $this->getTable()->insert($data);
return $toReturn->id;
}
}
RegisterPresenter.php
<?php
use Nette\Application\UI,
Nette\Application\UI\Form as Form;
class RegisterPresenter extends BasePresenter {
/** @var Users */
private $usersRepository;
protected function startup() {
parent::startup();
$this->usersRepository = $this->context->usersRepository;
}
public function renderRegister(){
}
protected function createComponentRegisterForm() {
$form = new Form;
$form->addText('name', 'Jméno');
$form->addText('email', 'E-mail: *', 35)
->setEmptyValue('@')
->addRule(Form::FILLED, 'Vyplňte Váš email')
->addCondition(Form::FILLED)
->addRule(Form::EMAIL, 'Neplatná emailová adresa');
$form->addPassword('password', 'Heslo: *', 20)
->setOption('description', 'Alespoň 6 znaků')
->addRule(Form::FILLED, 'Vyplňte Vaše heslo')
->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaků.', 6);
$form->addPassword('password2', 'Heslo znovu: *', 20)
->addConditionOn($form['password'], Form::VALID)
->addRule(Form::FILLED, 'Heslo znovu')
->addRule(Form::EQUAL, 'Hesla se neshodují.', $form['password']);
$form->addSubmit('send', 'Registrovat');
$form->onSuccess[] = callback($this, 'registerFormSubmitted');
return $form;
}
public function registerFormSubmitted(UI\Form $form) {
$values = $form->getValues();
$new_user_id = $this->usersRepository->register($values);
if($new_user_id){
$this->flashMessage('Registrace se zdařila, jo!');
$this->redirect('Sign:in');
}
}
}
Editoval Zuben45 (25. 9. 2013 21:46)