Saturday, September 7, 2013

Using Yii Framework

I have downloaded the Yii plugin for NetBeans, but first I had to install NetBeans 7.3.

Download the .nbm file for the plugin.
Go to Tools -> Plugins and add a plugin.
Navigate to the location of the .nbm file.

Immediately after installing the plugin, I noticed that there were some updates for it.
After update, Yii was no longer installed. Actually, it is still there, but it is part of PHP. Click 'show details' to see that Yii is installed.

Yii documentation is asking me to set the path to yiic.php. I cannot find it on my Mac. Ignored it and continued.

Create new PHP project in NetBeans and set the framework to Yii. It is taking a loooonnnngggg time to create the project. After 5 minutes, I closed the new project wizard. Lather, rinse, repeat. I am able to open the project. When I view properties, it indicates that yii is part of the project.

I am trying this tutorial: http://www.larryullman.com/series/learning-the-yii-framework/

Now I see. The plugin is not Yii. Yii must be installed separately. Download Yii from http://www.yiiframework.com/download/. Lather, rinse, repeat.

In NetBeans, set the path to yiic.php from Preferences -> PHP -> Yii.

Create new PHP project, select PHP framework. Project has been created with lots of files.

Uploaded framework and requirements folder to remote PHP server. I accessed /yii/requirements to test the PHP installation for conformance with Yii. All passed, with some warnings for databases that I am not using.

PHP does not run out of the box with NetBeans. It is necessary to install PHP, Apache and MySql. Download MAMP at http://www.mamp.info/en/index.html.

Tutorial for installing: https://netbeans.org/kb/docs/php/configure-php-environment-mac-os.html.
There is no MYSQL option in the Services -> Databases. Trying to add new connection.

I already had a MYSQL server set up. I deleted it and was able to access the server config window.

Set up admin properties for MAMP, not MySql tool. Am able to start/stop mysql from IDE.

Created PHP project. Created index.php in Source Files. Ran project. Index file appeared.

Created a Yii project. Be sure to set the path to yii.php or the require statement will not work and the page will be empty.

Configured Yii to use Gii to create models, views, controllers. When creating the model, I had to rename some columns in the database that had embedded spaces.

http://localhost/appname/index.php/gii/default/index

Gii created the model, but only in the destination folder, not in the NetBeans source folder.

Created new NetBeans project in htdocs of Apache. Now I can use Gii to generate code. I do not like using htdocs. I will add alias that points to location of PHP projects.

Alias /php "/Users/xxx/yyy/zzz/php"

I had to stop Apache before adding the alias.

I edited the URL for the configuration (config/main.php) for the PHP project to include the new alias.

When I use Gii to generate code from the URL, the NetBeans project is updated. The source for the project is in the user folder, not in the MAMP application folder.

I bought the book Yii Application Development Cookbook Second Edition by Alexander Makarov. It has more information than the above tutorial. The tutorial allowed my to get my app running, but the book is showing me the inner workings of Yii.

Be careful not to set up a general route like => website/page, as it will prevent gii from running.

 MAMP has phpMyAdmin.

Edit the protected/components/UserIdentity.php file to add authenticated users. Modify the accessRules in each controller to give access rights to each user.

Modify .htaccess

Modify .htaccess in the root of the project folder, so that index.php does not have to be added to each URL. I had to modify the Apache server httpd.conf to allow options to be overridden in .htaccess.

Options +FollowSymLinks
RewriteEngine on

RewriteBase "/alias-to-php-projects/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) php-project-name/index.php/$1

Place this in the .htaccess folder that hosts the yii application.

Alternate Rewrite

Instead of repeating this in all the .htaccess files, I used a Directory tag in httpd.conf for Apache. I created an Alias to a folder that contains all my Yii projects, then add the rewrite for that folder.

  Alias /php "/Users/xxx/yyy/zzz/php" 

   
   
        Options +FollowSymLinks
        RewriteEngine on

        RewriteBase "/php/"
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d  

        RewriteRule ^([^/]*)/(.*)$ $1/index.php/$2


   


The regex strips out the first part of the path and treats it as the name of the project, then appends index.php, then appends the rest of the path.

 Revisited Several Months Later

Several months after the initial controller, I wanted to create another one. Of course, I had forgotten everything!

Important files:
  • components/UserIdentity for adding users
  • config/main.php for defining URLs and other configurations
  • controllers
  • models
  • views

Renaming PHP project for NetBeans

To rename the PHP project, I had to copy the project to a new folder with the new name, then use the new project in NetBeans and delete the old one.

Change the project URL in the Run Configuration of Properties.

This does not work for a yii project, since the model was created for the original app.


































No comments:

Post a Comment

Followers