To begin with, we need to create a table, let it be known users, sql-query is shown below.
Next create a model in your application / models / and call it Users.php. This class will extend Zend_Db_Table
For experimentation we still need a separate controller, which will be called Auth, you can create a simple ZF Tools and handles.
If you are using NetBeans as a development environment, the beta version 6.9 is the ability to interact with ZF Tools directly from the IDE.
Then, create the 4 methods in this model: login, register, and outputs a page that displays the name of the user after successful login. Which will be called respectively login, signin, logout, home.
For those who prefer to make all handles, you need to create methods in addition to the model yet, and templates for display in your application / views / scripts / auth /
Create the shape LoginForm.php, which will create a form to access the site.
Next, add a form to register new users RegistrationForm.php. All forms, as you might speak of, must be placed in a folder APPLICATION_PATH / forms
In these forms there is no absolutely nothing interesting, except a few items and set them Dumb values??, for example, to be filled out and label (label).
Let's create our controller, the methods which will be responsible for entry and registration of users, as well as their output. First, give the entire text, then try to focus on more important points.
Method homeAction (): fist, we create an instance of Zend_Auth_Storage_Session, which will store information about a user who tries to enter the site or nai already logged. The object of this class returns a non-empty account if the user logged in, null otherwise. If null, redirect user to login to the site to enter your user name and password. Next in the file views / scripts / auth / home.html write the following:
To the user, passing the entrance to the site and saw your name and a link allowing exit (log out is not a good word).
Method loginAction (): in the first 2 lines, we instantiate our model, Users, and forms to access the site. (because of an incorrect first chosen method of naming the classes have to plug them in the initialization method, it is my fault, and in subsequent notes I will try to fix it). Third row we associate with our form of variable form, which displays in the template.
Next comes the business logic directly. First, we check that the data has been sent POST request, then check their validity. If these two conditions are fulfilled, we obtain values of the form
Create an instance of Zend_Auth and authentication adapter $ authAdapter.
I use the adapter, which is directly linked to a database provided by Zend'om. Next we indicate which fields are the username and password are.
And trying to perform authentication on a form sent from the data, the method -> authenticate ()
If the result is valid authentication -> isValid () (although we found a record in the database), store the result in the session and refer the user to the home page. If authentication fails, print an error message.
A template for this method. application / views / scripts / login.phtml
Method signinAction () doing about the same, except for the withdrawal of another form, checking passwords (whether they are the same?) And write data to the database a new user, if no errors occurred. This method is referred to isUnique () method, which we now append to Users.php, returns true, if specified for the name of the database exists and false otherwise. What will prevent the creation of two polzovatley with the same name. File application / models / Users.php.
The beginning of a serious project should be, although there are still wrong in the code, but with the experience I have, hopefully, get rid of them.
Good luck to you programming!
form; ?
|