Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Edward

My Project Diary

Recommended Posts

That what I'm going to do now write my own framework. I don't want to use yii because it's not my code. I am going to extend the mysqli class and add my own methods.

Share this post


Link to post
Share on other sites

Ha, don't think i will be putting it up on github. How about your project, why don't you post up your source files so we can have a look.

 

I just want to clarify reasons i won't be using a framework.

 

1. I will never be in the drivers seat if I were to use one.

2. I believe frameworks are better if you are a web developer and need to finish projects quickly.

3. I have the time to correct all errors in my own framework and i enjoy learning, if i use another framework it bores me.

4. Since a framework contains 1000's of files if someone inserts hacking code into a file, chances are you will never find it.

5. Most of the framework files you don't even require, this means extra loading time and a slower system.

6. I also see people asking stupid questions about how to do basic practical php with frameworks.

7. They only work one way and for some of the things i need to do, its going to take a long time to figure out how to get that framework to work for my task.

8. Lack of documentation for some frameworks, which means waiting and depending on others that know.

9. Frameworks will die out eventually or the current version, and when the current version dies out, there will be no more development for it. Whereas our projects live on.

10. You will never know how everything works exactly without spending thousands of hours, whereas with your own code your knowledge will be solid.

 

Anyway i just think for my project frameworks will not be necessary, i am not saying that they are not good for someone else. If i was a web developer and i had to get some projects done quickly i would definitely use it. But on the other hand you can never win full customization of php for your own project, its going to be a hell of a lot faster, and are definitely going to feel much happier with the code.

 

Tuesday, August 28, 2012

 

Built up most of my mvc now, had to deal with some small url issues, now i have come across another one, if i type in localhost/index page will load the css and image files, but if i type in localhost/index/ the css and image files will not load. So i am dealing with the slash problem, files seem to be trying to load of the next directory. Will probably figure this out quite soon, haven't checked online yet, but its an interesting mvc problem.

Share this post


Link to post
Share on other sites

Monday, September 24, 2012

 

What can i say, well Ive done quite a bit of work since my last log, but i guess that's what you would expect, huh? Okay Ive coded 12 dynamic PHP pages of my site now, i am finding things are quite easy for me now, pretty much anything i code works 100% first time. Larry PHP and MySQL book has really helped me a lot, i think the hardest part of my site was probably logging users in with all there info and listing products into the site. Once this stuff was completed most of the actions of the site were just updating or view info from the database by triple or quadruple joins, sounds hard but its actually quite easy. Most of the things i thought were difficult with a bit of practice just become a second nature.

 

I did loads of validation and quite serious code at the beginning of my site, this was necessary for practice, now i know if i wanted i could just validate or add filters anywhere. But what i done now is just code in pages and leave this stuff out because the practice version of my site will not be final. I felt that it would be a better idea to see how the data was fetched and manipulated page by page. The other advantage is i am seeing repeated code i mean code being used more than once, i can see code that can be added into a User and Product class. So what i am saying is i am getting an idea for classes that i can construct to help me with the site for a final version.

Share this post


Link to post
Share on other sites

Monday, September 24, 2012

 

I am currently using Dreamweaver CS5.5 i am aware CS6 has been out for a while now, i just ordered the new Adobe Classroom in a book for Dreamweaver CS6 and Illustrator CS6. I will be upgrading to CS6 in October. I have read books before to learn Fireworks and Photoshop, but i gather and i have heard from other web designers that Adobe Illustrator is the best tool for making web graphics as it pixel perfect. So my plan is to learn this Illustrator CS6 100% along with CSS which my skill is weak in now, and when i talk about graphics, i want awesome graphics for my website, its important to me. From what i can see i think CSS needs a hell of a lot of practice/experience to get some great CSS3 graphics, and i think its going to be a lot more work than learning PHP as its a form of art. Maybe some of you will disagree but for me anyway Art is one of my weak points, i am stronger with code logic.

 

I am currently using Blueprints grid framework CSS yes its awesome easy to use and understand, but its just foundation, i am gonna have to add a down of my own id's and classes for it really to be awesome.

Share this post


Link to post
Share on other sites

Monday, September 24, 2012

 

Despite what i said in my earlier posts about frameworks i have decided it would be best i use one. I think when we are building a web site we need something concrete to build in that is stable tried and tested a framework can offer that. Last thing you want is your web site crumbling beneath you. The other reason is the documentation, if i make my own php framework and work into it, how am i going to explain to a co worker how it all works, at least with Yii they can read their documentation or even Larry's new ebook to see how it works, documentation is something i don't have the time to write.

Share this post


Link to post
Share on other sites

Monday, September 24, 2012

 

Currently i have been working on the Modern Javascript Develop and Design book, i am up to page 250, i am working through 20 pages per day from Monday - Friday. The book is easy to understand but js has odd behavior unlike PHP, many things there that can trip you up as Larry says. In the other half of the day i am building my practice version of my website. I am not doing review and pursue in the js book, for me i think its a waste of time, my aim is to get this book completed as soon as possible. Ive got plently of pages in my site waiting for js to be applied to, so as soon as i learn these particular parts, i will be coding them into my site, so that's my review and pursue, the real thing. From what i can see so far is most of the JS stuff i need is basic, objects and stuff like that there would be no need for as PHP will handle this stuff on the server side.

Share this post


Link to post
Share on other sites

Wednesday, October 31, 2012

 

Happy Halloween!

 

Today i briefly finished the frameworks chapter of the Modern Javascript book, I worked through all the other chapters up to this but i feel now i will not benefit on going on further with the book. I still have my practice site, so now i think its time to get some of this straight JS implemented into the website to test it out. JQuery looks awesome but i still would rather complete the JS functionality from straight JS before using any JQuery so i am 100% about what is going on here. The way i like to do things is practice with my own code and then after come back to the Modern Javascript book and do the putting it all together chap, this way i can see if what i done was correct when i compare of Larry's code, it will also be easier for me to approach as up till now i have had zero js coding practice.

 

Some of you will either be doing or remember Effortless E-Commerce, well there is also that now i have some basic PHP knowledge, i will work through Larry's Coffee shop to analyze the good techniques used and see how these can be adapted into my own site.

 

The Yii Book has just come out today, i am working through this, first four chapters look like a breeze but i intend on keeping up with this as this will be my main project base.

 

Anyway I've got quite a bit to be getting on with and i almost forgot i am also working through the Advanced PHP book i need another two weeks to get this done up to what i need for now.

 

Well once i get more updates i write in my log but its busy busy busy for now.

Share this post


Link to post
Share on other sites

Sunday, November 18, 2012

 

I completed PHP Advanced and Object-Orientated Programming book early last week, i found it easier than Larry's first PHP book. A lot has changed for me over this week, i did have another go at building a fresh mvc oop framework comparing off one online and Yii's code, this may seem easy to start of with but its just start becoming a lot more work than i had initially anticipated. The point being if i keep going with a MVC of my own i will probably spend more time developing it than making my own site which now doesn't appear to be so complicated as i may have thought with the Yii Framework. In the middle of the week i had such a big headache i was very confused what i should do now, the problem with my brain is when i try to handle and think about too much at once it seems to shut itself down and just completely refuse learning similar to shorting a wire. I was thinking of the Yii Framework, building further my procedural practice site, javascript etc and it just became too much.

 

So i made a decision to completely change my approach, time to surrender making the MVC's and concentrate on development. Yii and the way it works is completely different to my procedural practice site, so i have also decided to just abandon this practice project completely and focus primarily on Yii. I don't have the time to build a system as well as develop a website, so that's how i can make my decision.

 

I am now waiting on Larry's Yii Chapters coming out, i have to say myself that i kind of like the wait, it gives me time to experiment around with Yii without the rush. This Monday i will start to work through Alexander Makarov Yii 1.1 Application Development Cookbook, ive read a bit of it and its good. So with Larry's Yii Book and this book combined learning Yii should be easy i should think, I hope and we shall see.

Share this post


Link to post
Share on other sites

Thursday, November 22, 2012

 

Yes i made the right decision of just concentrating on Yii and dropping my other projects as Yii is taking some time to learn and is much more difficult than actual php itself even though it is php

 

I created a user model using Gii today Yii code generator, its not really necessary to use as you could just create the model yourself but it does safe time. I have about 4 tables which are used for a user registration but what i done is put all the items into one table for now and took some out as i just wanted to practice with Yii because i have no idea how to join up tables in Yii so for simplicities sake it is best for this stage of learning.

 

I worked with some of the validation classes that yii provides and have done edits into the User model rules() method. Some of you might find this useful. Here is what i have done so far.

 

public function rules()
   {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
	    // match is the alias for CRegularExpressionValidator
	    array('username', 'match', 'pattern'=>'/^[A-Z0-9_]{4,20}$/i', 'message'=>'Please enter a valid username!'),
	    array('first_name', 'match', 'pattern'=>'/^[A-Z \'.-]{2,50}$/i', 'message'=>'First name must be between 2 - 50 characters!'),
	    // lenth is the aliax for CStringValidator
	    array('last_name', 'length', 'min'=>2, 'max'=>50, 'message'=>'Last name must be between 2 - 50 characters!'),
	    // email is the alias for CEmailValidator, CheckMX checks to see if email domain is valid
	    array('email', 'email','checkMX'=>true, 'message'=>'Please enter a valid email'),
	    array('address', 'length', 'min'=>2, 'max'=>100, 'message'=>'Address must be between 2 - 100 characters!'),
	    // type is the alias for CTypeValidator, date is the alias for CDateValidator
	    // I checked birthdays and found i couldn't enter a data lower than the year 1900 but I could enter 2013 which is not correct as no body would of been born yet.
	    array('birthday', 'type', 'type'=>'date', 'dateFormat'=>'MM/dd/yyyy', 'message'=>'Please enter a valid birthday in the format (MM/dd/yyyy)!'),
	    array('username, first_name, last_name, email, password, address, city, state, province, zip, country, birthday', 'required'),
	    //array('first_name, last_name, email, city, province', 'length', 'max'=>50),
	    //array('password', 'length', 'max'=>64),
	    //array('address', 'length', 'max'=>100),
	    //array('state, zip, country_id', 'length', 'max'=>20),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, username, first_name, last_name, email, password, address, city, state, province, zip, country, birthday, registration_date', 'safe', 'on'=>'search'),
    );
   }

 

Things that i need to work on tomorrow are Scenario's and i need to work on a separate validation rule for the username as there are the swear words and also copyright names which need to be blocked. I think a custom rule would be more appropriate for this matter.

Share this post


Link to post
Share on other sites
(...) and i need to work on a separate validation rule for the username as there are the swear words and also copyright names which need to be blocked. I think a custom rule would be more appropriate for this matter.

 

Don't matter with that. Delete/change usernames that violates such things. It's impossible to blacklist against bad usernames in general. I can still use "kuk" as a username, and you would not know what kind of curse words that is in norwegian.

  • Upvote 1

Share this post


Link to post
Share on other sites

Friday, November 23, 2012

 

Worked on User.php model class and setup a more appropriate validation rule for my username's.

 

public function rules()
{
 // NOTE: you should only define rules for those attributes that
 // will receive user inputs.
 return array(
	 // match is the alias for CRegularExpressionValidator
	 array('username', 'authenticateUsername'),
	 array('first_name', 'match', 'pattern'=>'/^[A-Z \'.-]{2,50}$/i', 'message'=>'First name must be between 2 - 50 characters!'),
	 // lenth is the aliax for CStringValidator
	 array('last_name', 'length', 'min'=>2, 'max'=>50, 'message'=>'Last name must be between 2 - 50 characters!'),
	 // email is the alias for CEmailValidator, CheckMX checks to see if email domain is valid
	 array('email', 'email','checkMX'=>true, 'message'=>'Please enter a valid email'),
	 array('address', 'length', 'min'=>2, 'max'=>100, 'message'=>'Address must be between 2 - 100 characters!'),
	 // type is the alias for CTypeValidator, date is the alias for CDateValidator
	 // I checked birthdays and found i couldn't enter a data lower than the year 1900 but I could enter 2013 which is not correct as no body would of been born yet.
	 array('birthday', 'type', 'type'=>'date', 'dateFormat'=>'MM/dd/yyyy', 'message'=>'Please enter a valid birthday in the format (MM/dd/yyyy)!'),
	 array('username, first_name, last_name, email, password, address, city, state, province, zip, country, birthday', 'required'),
	 //array('first_name, last_name, email, city, province', 'length', 'max'=>50),
	 //array('password', 'length', 'max'=>64),
	 //array('address', 'length', 'max'=>100),
	 //array('state, zip, country_id', 'length', 'max'=>20),
	 // The following rule is used by search().
	 // Please remove those attributes that should not be searched.
	 array('id, username, first_name, last_name, email, password, address, city, state, province, zip, country, birthday, registration_date', 'safe', 'on'=>'search'),
 );
}

 

I added the validateUsername() method to the model and added the following code in

 

public function authenticateUsername($attribute,$params)
{
// Create array of restricted words:
$restricted_words = array ('restricted_word1', 'restricted_word2', 'restricted_word3', 'etc');
// Create string for using in Perl-Compatible Regular Expression
$restricted_words = implode ("|", $restricted_words);
// Check for a valid username:
if (preg_match ('/^[A-Z0-9_]{4,20}$/i', $this->username)) {
if (preg_match ("/$restricted_words/", $this->username)) {
$this->addError('username','Please enter a valid username');
}
} else {
$this->addError('username','Please enter a valid username');
}
}

 

The code worked successfully. I have been reading more on the Yii Definitive guide and now it looks like i will have no bother, i can see whats going on. I am really starting to like the way Yii does things, I am someone that really needs to concentrate on the development than MVC system coding and that is definitely what Yii allows you to do.

Share this post


Link to post
Share on other sites

 

 

Don't matter with that. Delete/change usernames that violates such things. It's impossible to blacklist against bad usernames in general. I can still use "kuk" as a username, and you would not know what kind of curse words that is in norwegian.

 

No you couldn't because usernames are restricted to 4 characters.

Share this post


Link to post
Share on other sites

Think you got my point anyway. ;)

 

Btw. I would suggest constant instead of numbers/string right into methods. Much easier to change anything that way.

 

Keep it up.

 

There is a problem coming up about the constant idea. I made a const for an array of states this is within my address model class.

 

const US_STATES = array('' => 'Select State', 'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming');

 

Now i get the error "Fatal error: Arrays are not allowed in class constants". So how is the best way to go about this now. Larry if you have any idea's please let us know.

Share this post


Link to post
Share on other sites

Yes, constants (inside or outside of classes) must be scalar values. As for any ideas, what's the question? What is it that you're trying to do here?

  • Upvote 1

Share this post


Link to post
Share on other sites

Well I was trying to hold the us states in a constant array but from within the address model class. I ended up using a private property to hold the array for now. Do you have any better suggestion on where to put this or will this be fine. I also have my restricted words list that I need to use in many places.

Share this post


Link to post
Share on other sites

A private array is fine. It could even be static.

 

About the restricted words, I would place that in a config file and pass that array into the function from there. That way, It's a lot simpler to change.

  • Upvote 1

Share this post


Link to post
Share on other sites

Yes that would be the obvious thing to do, i know of the static arrays but for that i don't really think its the correct use as i won't be extending from the User model class and need to share the results in other classes. I'll use a private for now and move it later on, in our config section we have a main.php file that is a huge return array, the one you probably read about. I am sure Larry will cover where to add this stuff to pick up globally. I know for our own classes we add them to components and then can have the spl autoloader load them up so long as they have been configured in one of the files.

 

Today i am just extremely happy with Yii, the first few days i was a little slow with it but now the whole framework is kicking in, the more you learn the more awesome it becomes. I would say its easy to do stuff in i built my own registration form now with data going to two models, its got email compared fields populating dropdownlists and a captcha box to prevent spam. The whole Yii is not difficult like i initially thought the blog demo they have is confusing to start off with, what i done is left it then done my own coding in Yii then when i came back it then looked easy.

 

One thing in Yii that i am confused at today, is if we add an attribute to model for instance for captcha, email or password conpare field, even though we change rules etc the new attributes are not defined. But even the standard generated attributes i couldn't see the hard-coded versions just some commented out attributes. So anyway i had to add to my model extended active record class the attributes as public's to get the fields to work in my forms.

 

Yii makes things really easy, as you have form scenarios so you can set which attributes form a certain form should be validated or not. You can also allow user to be in certain pages or not by authentication. I would never go back to my old coding style after learning this, as it just saves a massive amount of time and is very smart and logical. I am not scared anymore to just start building my project, i will be going forward from now, as Yii is easy to come back and change for instance if Larry does have a better way of doing something. For right now i will only work with database and code logic, i will do zero CSS or visual stuff until this is finished.

Share this post


Link to post
Share on other sites

Wednesday, November 28th, 2012

 

I have been working on my Yii user controller and added in an actionNew() method, this part of code is for adding a user and there address details to the database. For some reason the foreign key constraint ON DELETE CASADE ON UPDATE NO ACTION, failed to work and stated that i was unable to add or update a child record. I will work on this problem tomorrow. As for the code below it works successfully without the constraint for the time being.

 

public function actionNew()
{
 $modelUser = new User('Register');
 $modelAddress = new Address;

 if(isset($_POST['User'], $_POST['Address']))
 {
  $modelUser->attributes = $_POST['User'];
  $modelAddress->attributes = $_POST['Address'];

  $modelUser->validate();
  $modelAddress->validate();

  $transaction = Yii::app()->db->beginTransaction();

  if($modelUser->save())
  {
   $modelAddress->user_id = $modelUser->id;
   if($modelAddress->save())
   {
  $transaction->commit();
  $this->redirect(array('view','id'=>$modelUser->id));
 }
 else
  $transaction->rollBack();
  }
 }

 $this->render('new',array(
  'model'=>$modelUser,'model2'=>$modelAddress,
 ));
}

Share this post


Link to post
Share on other sites

Looks very good. :)

 

Looked some at the API. Found that there: "For models with autogenerated primary keys, the value of $this->id is available while afterSave() is running." Maybe you can let the models handle all this. That would be sweet.

  • Upvote 1

Share this post


Link to post
Share on other sites

Yes it is pretty cool, the problem is though i have a transaction involved there so I am not sure about using afterSave() for this. Remember what facebook said though done is better than perfect.

 

I will starting the Yii Application development book again next week as i can see that does provide some extra tips. You know how you get brain freeze when you drink an ice drink too quickly, well i get the opposite when trying to figure this out alone, i call it brain burn. Reading some extra books and not just trying to work it out myself will ease on the real coding.

 

About testing, Ive done unit testing before with phpunit but now i am starting to see the importance of these unit tests. They are useful as they can be stored in the framework and can also save time. Ive wasted a lot of time in the last few days re-entering data into a big form. The Yii application Development book is real heavy on unit tests from the beginning of the book, i think that book is actually pretty good but i really people with more experience would understand the real benefit of it.

Share this post


Link to post
Share on other sites

Thursday, November 29th, 2012

 

protected function beforeSave()
{
    if ($this->isNewRecord) {
  // Set registration date and last activity
  $this->registration_date = $this->last_activity = new CDbExpression('NOW()');
  // Create the activation code:
  $this->activation_code = md5(uniqid(rand(), true));
  // Set correct birthday format (MM/dd/yyyy) -> (YYYY-MM-DD)
  $this->birthday = date("Y-m-d", strtotime($this->birthday));
 } else {
  // Set last activity if account updated
  $this->last_activity = new CDbExpression('NOW()');
 }
 return parent::beforeSave();
}

 

Thanks to Larry's Chapter on models i got the beforeSave() method to work in the model class, from the website the function failed to work. As you can see above i set the account activate code here which will be a md5 32 characters long string which will be used to validate an account. I am saving date of birth or birthday in my records when i first tried this on yii it didn't work and saved 0000-00-00 so i had to convert the Yii formatted date over to the SQL YYYY-MM-DD format for it to save.

 

I have to send an email to the user with a link to which the click to come to the activate page but there is no documentation i could find on the YiiFramework help explaining how to do this so i have left that bit for now. I would be implementing this code into the afterSave() method in the model class once i know how to do it.

 

Right now i am off to read more on Larry's model chapter good bye for now. :)

Share this post


Link to post
Share on other sites

Saturday, December 1st, 2012

 

I can't believe its December already, its like summer here i hadn't realized the time has gone so quickly. I was working on Yii yesterday but didn't get a smooth run, this will be the last time i say this but i think the Yii documentation sucks. They have stuff all over the place, its hard trying to figure out or get basic examples of some basic php stuff and how to do it in Yii. I had a lot of success working how to do things in Yii through the Yii errors and debugging.

 

In the last two days in the time i had i was working on my activation page a bit like the one i done earlier in my practice site but this time i will add in more security checking. As i understand we don't need to use real_escape_string() as PDO does this for us and whatever you use DAO, Query Builder or Active record it will be done as its all run on PDO (PHP Data Objects). The code below is not completely finished yet and also i know i need to move some of this code logic from the controller into the model. I am still waiting to see how Larry does this in his chapters, so now i know there is not also "Performance Tuning" in Yii, there is "Larry Tuning", this is a new Coding terminology i have come up with today.

 

Okay here is the code and today was the first time i used Yii Query Builder,

 

public function actionactivate(){// TODO Get the Activation page completedif(isset($_GET['x'], $_GET['y'])){// SQL "SELECT id, first_name, activation_code FROM user WHERE email='$_GET['x']"$row = Yii::app()->db->createCommand(array(	 'select' => array('id', 'first_name', 'activation_code'),	 'from' => 'user',	 'where' => 'email=:email',	 'params' => array(':email'=>$_GET['x']),))->queryRow();// Redirect if activation code is already null or no rows were found where sql will return falseif ($row===false || $row['activation_code']===null) {// Redirect to site index page$this->redirect(Yii::app()->user->returnUrl);}} else {// Redirect to site index page$this->redirect(Yii::app()->user->returnUrl);}$modelUser = new User('Activate');$this->render('activate',array('model'=>$modelUser, 'data'=> array('first_name' => $row['first_name'],'email' => $_GET['x'],'activation_code' => $_GET['y'])));}

ill will finish off the post part tomorrow and Monday.

 

I have to work on this part this can be done with routes.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...