Category Archives: Yii 2

Laravel 5.4 For Beginners Book Released

I’m really excited to announce the release of my latest book on Laravel, Laravel 5.4 For Beginners.  This book represents a huge leap forward for me both as a coder and a writer, as we dig deeper into the Laravel framework.  I covered a lot of ground that I hadn’t previously covered, and the book is as of this writing at 763 pages.

In theory, it would be simpler for me to just provide an upgrade guide for the previous version and just update the old book, but that doesn’t work for a number of reasons.  One reason is that the Facebook one-click registration and authentication implementation relies heavily on the traits that ship with Laravel, and those change with every release.

Also, with this release, Laravel no longer ships with Elixir, but comes with Laravel Mix instead, so I had to rewrite all of those instructions as well.

Of course I’m not satisfied with just an updated version of the book, I wanted it to cover new ground.  So one of the big new sections of the book is chapter 14, where we build a working chat application that updates in realtime.

For that we use a mix Laravel Echo, Vue.js, and Pusher.  This is a fun build and we get to learn more about nested components in Vue.js.

With all the work we do with Vue.js on the searchable, sortable data grids, and the additional work in the chat application, it’s a nice little quick start to Vue.js if you haven’t worked with that before.

Also new in this edition, we cover Laravel’s wonderful eventing architecture by creating a mail event on completed registration.  This takes advantage of the new markdown feature to make the email “pretty.”  And of course we send dynamic data to the email, like the username.

Besides covering all the basics, like we have in the past versions of the book, we also cover some of the interesting parts of Laravel’s architecture, like the service container, service providers, automatic injection, and the new on-the-fly facades.

I think the dynamically generated facades are going to have a huge impact going forward, it’s another wonderful innovation from Laravel.  I cover it briefly in the book, where we also cover service providers and class bindings.  We learn how to code to a contract rather than a concrete class.  That’s a little more intermediate, but we provide a gentle introduction to it.

I also thought it would be cool to support the chat application with a custom console command that you can run as an artisan command or as a cron.  The command deletes all but the most recent 30 messages, so you don’t overwhelm your application with chat messages.

To recap what we cover in the book, we build a sample application that covers the following:

  • One-click Facebook Registration
  • Full Access Control
  • Datagrid with column sorts, powered by Vue.js
  • Image Management with Intervention Image Package
  • Laravel Mix Asset Management
  • Events
  • Sending Email with Markdown
  • A working Chat application with realtime updates
  • Laravel Echo, Vue.js and Pusher
  • Plus all the basics of application development

For those of  you who purchase the book, I hope it helps you meet your programming goals.  I really appreciate all the support, comments, ratings and reviews I’ve received, since I launched this blog.  Thanks to my readers for sharing the learning journey with me. See you soon.

 

Free Book By Eric Borgos

One of the people that mentored me early in my career as an entrepreneur was Eric Borgos, a web wizard who made millions as a one-man operation. Eric recently wrote a book, How to Make Money Online, and he has generously made his book available to my readers for free. All he asks in return is that you review it on Amazon.

You can find the free downloads here for each version:

I can tell you from personal experience that Eric has wisdom to share. I have benefitted from that wisdom myself, his advice and encouragement helped me launch my own business career many years ago.

Eric’s book is a fascinating read for anyone who is trying to make money online, but it’s especially potent for developers because it gives you a sense for what can be done if you have skills. Eric wasn’t a programmer, he always had to pay for programming, but nevertheless he has an engineering sensibility that found efficiencies in interesting places.

I highly recommend this as a pleasure read for anyone who is working in tech, and that’s a very broad way of describing it these days, but I like it because it encompasses so much. If you ever get the chance to sit down with someone as brilliant as Eric is to pick his brain, take that opportunity. You will be amazed at how much you can learn. His book is getting a well-deserved 5 star rating.

Thanks again to everyone for all the positive reviews on Yii 2 For Beginners. Also special thanks to all the programmers from around the world who follow my blogs, all likes, comments, and reviews are greatly appreciated.

Yii 2 Para Principiantes Published Today, it’s Yii 2 For Beginners translated into Spanish

I’m happy to announce that Yii 2 Para Principiantes has been published today. This is Yii 2 For Beginners translated into Spanish language by Victor Hugo Garcia.

Victor put a lot of great work into this translation, including translating class names and DB tables into Spanish names when appropriate. The translation covers the core book, up through chapter 11. After that, the bonus chapters are provided in English and will be updated into Spanish as they become available.

Anyone who purchases the book, whether it is the Spanish Version or the English version gets free updates for the life of the book. All you have to do is login and pull down the latest version.

Like its English counterpart, Yii 2 Para Principiantes will take you step by step through setup and installation, and then on to coding in one of the most exciting PHP framework available today. The book focuses on creating a reusable template that can serve as the basis for your projects, including the following features:

      Setup and install
      A Working user model
      User registration and login
      User Profile
      Forgot password recovery
      Frontend and backend separation
      Helper classes
      Access control
      Free/Paid content control
      RBAC with backend UI
      JUI DatePicker
      Facebook Social Widgets
      Font-Awesome Implementation
      Image Upload and Management
      Multiple Social Auth Providers
      Custom Data-driven Carousel Widget
    Facebook Login & Registration with one click

I’d like to give a personal thanks to Victor, who not only gave me a great review on the book at GoodReads.com, but then later also volunteered to write the translation. He put a lot of work into it, so that is greatly appreciated.

Thanks again to all the programmers from around the world who have supported the book, it makes this work possible. I hope you find the book helpful.

Yii 2.0.4 Released

Today marks the release of Yii 2.0.4, which according to the release statement, is a patch release containing over 100 minor new features and bug fixes. I ran composer update on the template that we build in my book, Yii 2 For Beginners, and I’m happy to report that nothing broke, so no modification is necessary this time out.

I recommend updating to version 2.0.4. Like I said, it’s simple, just run composer update from the command line and you will get the latest version that will bring you up-to-date.

In other news, we have a Spanish translation of Yii 2 For Beginners being released soon, so that is very exciting. Victor Hugo Garcia is doing the translation and we’re looking forward helping Spanish-speaking programmers get started with Yii 2.

Learning a PHP framework can be a daunting task. I know this because it was difficult for me, and that inspired me to write the book. I’ve come to understand that learning a framework, much like learning programming itself, is a journey, not a destination.

Many times I thought of learning PHP in terms of one framework vs. another, and in some ways, that is what kicked off this blog back in March of 2014. However, my view on this has evolved.

I think it’s important to learn multiple frameworks. It will allow you to see programming from different angles, different approaches, and you will learn from all of them. Then you can decide which one is best for you, or even better, you can gain proficiency in multiple frameworks. This gives you more value to potential clients that might want you to do a project in a framework other than the one you prefer.

The programmers that build and support these frameworks, Yii 2, Laravel, Symfony 2 and others, bring an amazing amount of innovation, talent and infrastructure to support your growth as a programmer. As I mature as a programmer, I have come to value all of them for the contributions they have made to PHP programming. Where would we be without them?

So today, being a release day for Yii 2.0.4, I will thank the Yii team once again for all the amazing work that they do.

Yii 2.0.3 Released

Yii 2.0.3 was released today, and while that’s great news, it means I have to make sure everything in my book, Yii 2 For Beginners, is 100% working with the update.

So, today, I ran the composer update and everything went smoothly. In about 2 minutes, I was fully up-to-date and you have to love this framework for that.

Just for those who are unfamiliar with composer, the framework files live in a vendor folder, and when you run composer update, it reads your composer.json file and brings your vendor files up-to-date with the latest versions and dependencies.

So composer knew that once 2.0.3 was released, 2.0.2 was out of date, and it stripped everything out and replaced it with the newer version. What a fantastic way to maintain a framework.

The Yii 2 advanced template is somewhat of a unique case however, because the template code lives in the backend, common, and frontend folders, and that can change with a version release. Yikes!

This means I have to check every aspect of the advanced template to see if there are any changes that impact the re-usable template we build in the book as well as the out-of-the-box code, since I take us through the complete setup and explain things like the site controller and the related form models.

I have to review all that line by line. I’m happy to report only one minor change in the login method, which I quickly updated in the book, so as of now, we are 100% code working and 100% compatible with Yii 2.0.3.

For those following along in the book, no change is necessary due to today’s version release. I do recommend that you stay current on the latest release and do a composer update.

I’m happy to say that I got the full review of the release and the update of the book done on the day of the 2.0.3 release. I know from your comments, emails and reviews that this hard work is helpful to a lot of people.

The readers of the book have been helpful too, letting me know about grammatical errors and other typos. I typically respond within 24 hours to email requests. There hasn’t been any code breaking in quite a while and we’re closing in on 100% of the grammatical errors. I really appreciate the help and it makes a better reading experience for everyone else and this is helping drive the positive reviews.

Since no other action was required on my part due to today’s release, this gave me a chance to tighten anything up that was previously less clear and to cover anything unusual.

For example, a reader wrote in to tell me that he couldn’t get the date to work on the profile model unless he added the php prefix in the validation rule:

[['birthdate'], 'date', 'format'=>'php:Y-m-d'],

I couldn’t reproduce his issue, but I thought I would mention this in case you run into a similar problem and I updated the book to reflect that.

Anyone who purchases the book gets free updates for the life of the book, all you have to do is login to your leanpub.com account and download the latest copy.

For minor issues, like the above, I generally don’t make email announcements. However, I’m also working on bonus material that I’m adding on a quarterly basis (approximately) and I have more new material coming soon, so there will definitely be an announcement with that release.

The core material in the book is 100% complete, and you can get up and running quickly with it. But the template we build in the book is also one I use personally, so I’m always working on it, and it only makes sense to share it with you. Those updates are free for the life of the book.

The other thing notable to me about today’s Yii 2.0.3 release is that this is the third minor release since the 2.0 release 4 months ago. This means you can have confidence that the Yii 2 framework is being very actively developed.

In their own words, Version 2.0.3 is a patch release of Yii 2.0 which contains about 50 minor new features and bug fixes. These include some performance optimizations for RBAC, Page caching for RESTful repsonses, and more. You can read about it here.

Even though a version release for Yii 2 is a hectic time for me, I really do enjoy it. I love the architecture of the framework, and reviewing it for a version release reminds me how easy it really is to use and how it just keeps getting better.

Let me end with a special thanks to all the programmers from around the world who participate in the this blog by reading the aricles, taking the polls, leaving comments and writing reviews. It is very much appreciated and keeps me working hard to continuously improve. Thanks again.

Yii 2 Learning Your First PHP Framework

I’d like to start today’s post with a big thanks for the positive reviews I’m getting on GoodReads.com, please add yours if you can.

Also, a big thanks to everyone who has emailed me with supportive comments for the Yii 2 For Beginners book and for the positive comments on this blog.

I’ve made it my mission to help introduce people to the wonderful Yii 2 framework, and I know from first hand experience how hard it is to get up and running on something as big as a PHP framework, especially if you’ve never worked with one before.

The decision to use one framework over another is a personal choice. The major frameworks that I talk about, Yii 2, Laravel, and Symfony, are all great frameworks. You have to pick the one that suits your development style and this can certainly be a challenge when you haven’t worked with them because how are you supposed to know how well it fits?

Well, it’s like expensive clothing, you won’t know if it fits until you try it on. Obviously I’m recommending Yii 2 as the first choice among the frameworks from my point of view. Here’s why:

Top 10 reasons I would start with learning Yii 2:

1. Yii 2’s Advanced Application Template. This template comes with a working user model that includes registration, login, and forgot password functionality right out of the box. It also includes backend and frontend separation, so that you are clear on how to structure your admin area. You can get up and running quickly.

2. Gii. Yii 2 has an amazing code generation tool named Gii, which helps you stand up your models, controllers, and views quickly. The generated code almost always needs refinement, but once you know how to do that, things move along with lightning speed. It takes about 2 minutes to generate 8 files, a model, a search model, controller, and 5 view files. You’ll be amazed how far you can go in so little time.

3. Clear MVC pattern. Yii 2 has folders named models, controllers, and views, so the application structure is just what you would expect from an MVC.

4. No Routing. Most of the other frameworks require you to explicitly name the routes through the site, which can be tedious and at times confusing. Yii 2 handles this automatically. Of course you can set things like pretty URLs and any other customizations that you need, but the basic routing is handled by the framework. This is makes it very easy and intuitive to work with.

5. Widgets. Yii 2 supplies an incredible number of widgets to help develop your application. They cover everything from GridView, Pagination, Forms, DatePicker, etc., it really is impressive what you get out of the box. The widget class is easy to extend, so you can quickly build your own widgets. And once you start using these, you will get spoiled very quickly because you will want to use them for everything.

6. Mobile-First Bootstrap. So right out of the box, Yii 2 incorporates Twitter Bootstrap, which of course is device-responsive, so you can jump into mobile-first design right from the start. Wouldn’t you love to pitch that to your client?

7. Validation Made Easy. Validation in Yii 2 is handled on the model, with a very intuitive Rules method that simply holds an array of settings. The settings include the name of the attribute you wish to validate, the validator, and the options.

For example, do you need to make your email addresses unique so two users can’t have the same email address? It’s one line added to the Rules method.


['email', 'unique'],

It’s that simple.

8. Behaviors. Yii 2 incorporates a method named behaviors in both the models and controllers and this is another intuitive winner. In models, behaviors are used to manage events, like automatically inserting timestamp for creating and updating records, which is a useful behavior for a model to have. You can see how intuitive that sounds.

On Controllers, behaviors help you manage access control, things like restricting pages to logged in users only. Again, very intuitive and easy to work with. You can reference some of the tutorials on this blog for examples.

9. Native ORM. So first, what is ORM? According to wikipedia:

Object-relational mapping is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.

It’s a perfectly scientific explanation, but I don’t find it particularly helpful. We can break it down to something more digestible.

In the simplest terms, the ORM manages how your application communicates with the database, greatly simplifying the syntax. Example:

return $users = User::find()->asArray()->all();

The above code returns all the users from the user table in an array. No having to open a connection, no SQL, no binding parameters, no specifying fetch mode, etc. It’s just one line that reads like a sentence. Look at how simple that is!

Because Yii 2’s Active Record ORM is native, it is integrated seamlessly into the framework, and this is one of the reasons why Gii is so effective at generating code. The other frameworks use Doctrine, which brings with it the excess baggage of their repository structure etc., and in my view, making it more complicated and less efficient.

10. Enterprise Capability. If you are just starting out with framework development, you are probably not ready to jump into enterprise development. But wouldn’t it be wise to learn the framework that can take you there? Learning Yii 2 ultimate builds value into your career as a programmer.

Ok, so that’s my short list of why I would start with Yii 2 as opposed to some of the other choices out there.

The key to learning a PHP Framework, if you have never done this kind of development before, is patience. Yii 2 hands you an amazing amount of features, but you have to be willing to take the time to learn them.

Another key to success in programming is this: Don’t quit. Just keep going until you get it. You will get it.

Yii 2 has a great community that is willing to help you. Also, I wrote a book that can get anyone with a moderate amount of PHP skills up and running quickly.

I’ve also augmented that with free tutorials on this blog, and I’ve also included bonus material into the book. Everyone who purchases the book gets the free bonus updates for the life of the book. I plan to keep working on it for the foreseeable future.

Working with a PHP framework is a journey, not a destination. It’s hard work but it can also be a lot of fun. I’m very happy to share this journey with you.

Thanks again for all the positive comments and reviews. As always, any comments, reviews, word-of-mouth referrals, are greatly appreciated.

Yii 2 Gii code generation

One of things about Yii 2 that is drastically different from some of the other frameworks is Gii, it’s code generation tool. I cover the use of Gii extensively in my book, Yii 2 For Beginners.

On the surface, this might just seem like a fun feature to use, a time-saver in certain circumstances. And that’s probably true of a lot of code generators, they only take you so far. But Gii is different.

Gii is so powerful that it has very meaningful impact on work flow. And this impact reaches deep into our profession. You can look at it from two perspectives, one, the individual developer doing work for clients, and two, from a team perspective, which can bring you up to full enterprise level.

So let me talk first about how it impacts the individual, and since a lot of my readers are beginners, it will help them understand the importance of Yii 2 and Gii.

1. Planning. Use of Gii impacts planning. Before you code a single line of code, you can factor in not only the efficiency of Gii, but also the workflow.

For example, to start, you look at the scope of the project and begin sketching out the table structure. The more well-thought out this data-structure is, the useful Gii becomes. If you use foreign keys to relate tables, Gii will understand this and generate relationships in the models.

This gives you the ability to imagine the project, and better yet, diagram the tables in MySql workbench, before any PHP is involved. And once you know the number of tables you are working with, you will have some idea of how long basic development will take. This is possible because you can average how long each table takes to convert to model to controller to view.

In a typical implemenation, there are 8 files generated. And they also typcially need customization, but once you have an idea of what you generally use for that, even the 1st level of customization goes quickly. When I say quickly, I mean crazy fast. It’s awesome. Awesome is not a technical word, but you have to give me a pass on that one, I really love Gii.

2. Strong MVC Pattern. The code that Gii generates is a beautiful implementation of the MVC pattern. It’s probably the easiest structure to follow of all the frameworks because you literally get folders named models, controllers, and views. And once you understand how it’s all stiched together, it just makes perfect sense.

3. Learn Good Practices. As your workflow and project planning are impacted by Gii, you begin to develop a discipline to the structure. While you are certainly not locked into anything, if you follow the breadcrumbs of good practices that Yii 2 and Gii are putting out there for you, your project becomes very tight and focused. Things are always done the same way, at least at the base level.

It’s understood of course that I’m talking about the bones of the project, not the skin. Presenting the data in the views often requires a lot of customization, but of course Yii 2 helps you bigtime there too with its widgets. But the widgets will have to be the subject of another post.

4. Grow As A Developer. As an individual developer, you build value into your skill set by following best practices. Today you are an individual developer or student. Tomorrow, you may work in a team environment. And those looking to hire programmers are looking for programmers that can follow a discipline and they’re most likely looking for programmers who have framework experience. And the good news is that this is a very accessible framework. You can learn it and benefit from it.

And that gives us the perfect transition to talk about team development. All the benefits that individual developers get from using Yii 2 and Gii are amplified in team and enterprise development. While time estimates are also important to individual developers, they can be absolutely critical in a company environment, where commitments are made to clients or to the backers of a venture.

So you can look at the list of benefits like this:

1. Workflow Standardization. The planning and flow of the project always follows the same path. Team members are assigned specific roles, which is certainly not unusual, but knowing the power of the framework and the capabilities of Gii enhances the strategic deployment of those resources.

2. Common Starting Point For Code. If the conversation starts with data-structure, it will lead to using Gii, in most cases. Obviously there are exceptions to everything. But to the extent that Gii gives us a common starting point for code, it gives the security of building a proven structure. The tight association it creates between model, controller, and view is not something you want to go out of your way to break, unless you have very good reason to do so.

3. Rapid Prototyping. Gii gives you the ability for rapid prototyping. Companies rarely have the luxury of asking “what if?” But with Gii, you can do that. You can even set aside a prototype branch with a separate DB, and playout those whatif scenarios quickly. No company would ever do that unless they could do it quickly and efficiently. But having that ability can give you a competitive advantage, so this is great.

4. Security Of Standardization. Lastly, I should state one of the most obvious benefits to the standardization that both Yii 2 and Gii offer. What if someone leaves the team? Someone else will have to take over their responsibilities, and wouldn’t it be better to know that they can do that efficiently because the structure and discipline of Yii 2 and Gii were adhered to? Of course it would. That’s a big reason why companies use frameworks in the first place.

Besides all the reasons I stated about the benefits of using Yii 2 and Gii, I thought about why it is so good at what it does and why I haven’t seen a competing framework offer something as compelling, at least not from my point of view. And I think the answer is that Yii 2 has it’s own native ORM implementation, which just makes their code generation seemless.

Having their own native ORM also gives Yii 2 the freedom to structure their MVC implementaion how they wish, so they’re able to keep it intuitive, without having to make sacrifices because of 3rd party ORM or other considerations. I believe this is a huge advantage and is one reason why Yii 2 is able to offer so much out of the box. If you haven’t tried this framework, I would strongly encourage you to do so.

I’d like to take a moment and thank all the programmers from around the world who are reading my blog, I hope you are finding it useful. And thanks again to everyone who has supported my book. Feel free to contribute with your own comments, links, and reviews, it is greatly appreciated.

Yii 2 For Beginners Update

Happy New Year Everyone!  I hope 2015 brings you joy and happiness.  I’ve been hard at work during the holidays to take care of bugs and typos in my book.  Even though Yii 2 For Beginners is only 64 days old, there were some minor changes to Gii and some other minor points that prompted me to do a fresh install and build the entire application from scratch.  Plus, as some readers pointed out, there were a bunch of typos.  No more.  The significant typos are gone and the bugs have been squashed.

Because I went through the entire application, I was able to clarify little things that will end up saving you time.  Most importantly, I confirmed that the code is accurate.  Everything is 100% working as described in the current version of the book.  I’ll do my best to stay on top of Yii 2 changes that will affect the book as they occur in the future.

I’m also happy to announce a major enhancement to the book.  I’ve incorporated Gists, which are code snippets hosted by Github, directly inline and in context in the book.  Anywhere in the book where there are more than 3 lines of code that need to be copied, you can now launch a gist in a new window and copy it from there. This makes working with the book a lot easier. And you don’t have to worry about bookmarking anything, it’s all attached to the relevant parts of the book, so it’s all organized for you.

And the even better news is that all of this is free.  All updates to the book are free for the life of the book.  All you have to do is go to your http://www.leanpub.com account and download the latest version.

I’m also pushing hard to get bonus material written for the book by the end of January 2015.  I would have waited with the current release until then, but I know how important your time is, and I wanted to get the improvements out to you as quickly as possible.

Let me conclude this post with saying how much I appreciate the positive comments, links and reviews.  It really drives me to do more.  I absolutely love the Yii 2 framework and knowing that I can help people learn it, as others have helped me, gives great purpose to my writing.  So once again, happy 2015.  I’m looking forward to sharing a great year with you.