Autoloading Organized Routes in Laravel

This is an excellent tip from Jesse O’Brien about breaking up your Laravel routes file.  I’ve actually done something similar for a while now, and kind of forgot that Laravel doesn’t come preconfigured this way.

First, in the application directory, I create another directory called “routes”.

In that directory, I have a “filters.php” and “events.php” and some other files to hold my GET and POST routes.

Then in the start.php, I drop on this bit of code:

foreach (scandir(path('app') . 'routes') as $filename) {
	$path = path('app') . 'routes/' . $filename;
	if (is_file($path)) {
		require($path);
	}
}

This way, you can add as many routes as you want, and they’ll all load.

I’ve toyed with the idea of scanning for directories in the routes folder, and including those as well… but if you’re at that point, you should probably just use controllers.

Great Read – On Being a Senior Engineer

This is an excellent rundown about what being a “senior” engineer is all about.  Being part of a well-oiled development team, I really try to live up to these standards.  I especially like the “Ten Commandments of Egoless Programming” that he posts:

  1. Understand and accept that you will make mistakes. The point is to find them early, before they make it into production. Fortunately, except for the few of us developing rocket guidance software at JPL, mistakes are rarely fatal in our industry. We can, and should, learn, laugh, and move on.
  2. You are not your code. Remember that the entire point of a review is to find problems, and problems will be found. Don’t take it personally when one is uncovered. (Allspaw note – related: see below, number #10, and the points Theo made above.)
  3. No matter how much “karate” you know, someone else will always know more. Such an individual can teach you some new moves if you ask. Seek and accept input from others, especially when you think it’s not needed.
  4. Don’t rewrite code without consultation. There’s a fine line between “fixing code” and “rewriting code.” Know the difference, and pursue stylistic changes within the framework of a code review, not as a lone enforcer.
  5. Treat people who know less than you with respect, deference, and patience. Non-technical people who deal with developers on a regular basis almost universally hold the opinion that we are prima donnas at best and crybabies at worst. Don’t reinforce this stereotype with anger and impatience.
  6. The only constant in the world is change. Be open to it and accept it with a smile. Look at each change to your requirements, platform, or tool as a new challenge, rather than some serious inconvenience to be fought.
  7. The only true authority stems from knowledge, not from position. Knowledge engenders authority, and authority engenders respect – so if you want respect in an egoless environment, cultivate knowledge.
  8. Fight for what you believe, but gracefully accept defeat. Understand that sometimes your ideas will be overruled. Even if you are right, don’t take revenge or say “I told you so.” Never make your dearly departed idea a martyr or rallying cry.
  9. Don’t be “the coder in the corner.” Don’t be the person in the dark office emerging only for soda. The coder in the corner is out of sight, out of touch, and out of control. This person has no voice in an open, collaborative environment. Get involved in conversations, and be a participant in your office community.
  10. Critique code instead of people – be kind to the coder, not to the code. As much as possible, make all of your comments positive and oriented to improving the code. Relate comments to local standards, program specs, increased performance, etc.

I like to think I live up to most of those, though I’m somewhat (sometimes very) guilty of #9.

Laravel and Redactor

I’ve been working on this book thing, and the bit I wrote about using Laravel with Redactor is quite nice.. if I do say so myself.  Here’s a bit…

First, WYSIWYG text editor javascript libraries are pretty hit and miss. But Redactor is brilliant. It looks good, it’s coded well, and it just works. Using it with Laravel is a PHP dev’s dream.  So to start, we need to make sure we have a copy of Redactor and have Laravel all set up.

In our routes.php file, we create a route to hold our form with the Redactor field

Route::get('redactor', function()
{
    return View::make('redactor');
});

 

Then create a view named redactor.php.  I’m using straight PHP for the form, but Blade would work just as well.

<!DOCTYPE html>
<html>
   <head>     
         <title>Laravel and Redactor</title>
         <meta charset="utf-8">
         <link rel="stylesheet" href="css/redactor.css" />
         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
         <script src="js/redactor/redactor.min.js"></script>
   </head>
   <body>
         <?php echo Form::open() ?>
         <?php echo Form::label('mytext', 'My Text') ?>
         <br>
         <?php echo Form::textarea('mytext', '', array('id' => 'mytext')) ?>
         <br>
         <?php echo Form::submit('Send it!') ?>
         <?php echo Form::close() ?>
         <script type="text/javascript">
               $(function() {
                     $('#mytext').redactor({
                           imageUpload: 'redactorupload'
                     });
               });
         </script>
   </body>
</html>

So we created a textarea with the name ‘mytext’, and make the id of that field the same as the name.  So to target it, and add Redactor to it, just use

$('#mytext').redactor();

In the imageUpload parameter, we pass the URL path where we post the image. In this case, it’s routing to ‘redactorupload’.  So let’s create that route.

Route::post('redactorupload', function()
{
   $rules = array(
         'file' => 'image|max:10000'
   );

   $validation = Validator::make(Input::all(), $rules);
   $file = Input::file('file');
   if ($validation->fails())
    {
        return FALSE;
    }
    else
    {
         if (Input::upload('file', 'public/images', $file['name']))
         {
            return Response::json(array('filelink' => 'images/' . $file['name']));
         }
         return FALSE;
    }
});

The image will automatically POST here. So we want to make sure it’s actually an image and is less than 10 megabytes… so we run those validations. If everything validates, we move it to its permanent location, and send out a json response.  Redactor expects the json key to be ‘filelink’ and the value to be the path to the image.  If everything worked, when you add the image, it will display in your Redactor textarea.

We can check what the code output looks like by creating a route to accept the Redactor data.

Route::post('redactor', function()
{
    return dd(Input::all());
});

File uploads through redactor are done pretty much the same way, except with the fileUpload parameter… and the json output should also include a ‘filename’ key.

Postmark – Laravel bundle

I did a Laravel bundle for the Postmark API.  It’s really just a simple wrapper for the API, and I needed it for a non-Laravel project I worked on, and noticed there wasn’t a bundle already. Probably the one update that would be most useful is getting attachments working.  Right now, you have to parse the content and mime-type before attaching… and I should probably update it to just accept the file and do all the parsing in the method. Maybe one day.

Setting up a virtual host for WAMP and Laravel

Normally when I start a new web project, I just use “http://localhost/project_name_here” as the URL I run, and that’s worked for me. But recently, I started learning and working in Laravel, and one of the…uh… features, is that you have to navigate to the “public” folder.  So your URL would be “http://localhost/project_name_here/public”.  That was fine while I was learning, but I’m about to start a new project, and I don’t want that “public” in there.

I first thought about modifying the htaccess file and putting the “public/index.php” file into the root and changing the paths, but I decided to go the Apache VirtualHost route. Of course, since I’m using WAMP, most of the suggestions about VirtualHosts, like from Code Happy,  don’t apply… and searching for WAMP VirtualHost shows as many solutions as there are results.  So here’s what I did…

1) Go into your WAMP Apache config files.  My wamp is located a c:/wamp, so the path to my config files is C:/wamp/bin/apache/Apache2.2.17/conf

     a) open httpd.conf in a text editor
     b) located the line

#Include conf/extra/httpd-vhosts.conf

And remove the first “#”
     c)  go into the extra folder, and edit the httpd-vhosts.conf file.  Here’s what I added..

<VirtualHost *:80>
ServerAdmin webmaster@terrymatula.com
DocumentRoot "C:/Users/Terry/Dropbox/www/project_name/public"
ServerName project_name.dev
</VirtualHost>

<Directory "C:/Users/Terry/Dropbox/www/project_name/public">
Options Indexes FollowSymLinks
AllowOverride all
# onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>

     d) restart the Apache service

2) Edit the windows host file.
     a) go into C:/Windows/System32/drivers/etc and open the file “hosts” in a text editor.
     b)  Just add one line:

 127.0.0.1  project_name.dev

That got everything working perfectly for me.

Laravel PHP Framework – collection of screencasts and tutorials

A little over a month ago, I started toying around with other PHP Frameworks besides my first love, CodeIgniter.  I was familiar with Kohana, because of its CI ties, and I tried out Yii and even micro-frameworks like Slim.  Then, somewhere in my Twitter stream, I came across a tutorial for Laravel from a site by Dayle Rees.  There was also talk of an impending book release based on those tutorials, and when it was released, I happily bought a copy of Code Happy.  And while it could just be effective mind-control marketing on their part, I really did find it fun to learn and code.  So the following are some tutorials that helped me along the way to learning about Laravel…

Code Happy

If you want to get up-and-running quickly, even without previous framework experience, I can’t recommend this book enough. The minimum price is $5, but if you don’t give at least $10, you’re a big, stupid jerk. Seriously. There’s already been an update to Laravel, and he updated the book almost immediately after… and once you buy it, you get the updates for free.   Beyond the fact that this is an immensely helpful book, people need to support this publishing model.

You can also find most of the material on his blog, if you want to see what you’re getting.

 

HeyBigName – Shawn McCool

He caused a bit of an “uproar” over his Codeigniter is dead post, but his Laravel tutorials are top-notch. And screencasts to boot… which are always cool. The controllers and routes tutorials are great for beginners, and get you started making sites pretty quickly. I just watched his Form Model screencast, and I’m eager to try out that bundle.

 

 

The Official Laravel Screencasts

There aren’t a whole lot of these, but they serve as a great introduction to the framework. If you want to get acquainted with the Eloquent ORM, there are some good videos… and I found the overview of the Blade templating system to be informative.

 

Jason Lewis

There’s some more overview/getting started stuff here, but also a couple of more in-depth tutorials. The Advanced Validation lesson is very nice, and is something I’ve begun to implement myself.

 

Eric Barnes

Not so much tutorials, but he blogs about Laravel and uses it for his job. I subscribe to his blog for his links to other Laravel (and Codeigniter) resources and occasional commentary.

 

The Laravel website

This is a very young framework, but the documentation is very well done and extensive. One of the reason I love PHP and Codeigniter is because they are both well documented, and if I have a question about a method, or the order of arguments for a function, I know exactly where to go.  Laravel has taken this notion to heart right from the beginning.  And if you can’t find your answer there, the forums are a great place to ask your question. In fact, the guys that did these tutorials are frequently the ones to answer questions… so you’d be getting expert advice.

 

So eventhough Laravel is probably one of the youngest PHP frameworks, it seems to be gaining a lot of steam very quickly. People love it even if they don’t plan on using it. So hopefully, this list will only expand as more people jump on board.

Links to Unemployment signup page for each state

At SXSW, I spent a couple of the days at the sessions dealing with government topics.  One of the things that was brought up quite a bit was that State websites are almost universally horrible.  They’re poorly designed and finding information easily is near impossible.  Utah is one exception to this rule, but really, it’s just about the ONLY exception.

I decided to try and find the Unemployment signup pages for a couple of states, just to see how easy or difficult it was.  It was NOT easy.  I remember a couple of years ago, when I was laid off, getting to the Texas unemployment online signup was full of twists and turns, and pages of descriptions and explanation. I just wanted to go to the signup page.  So I decided to create a simple page with links to the unemployment signup pages for each state… hopefully skipping all the explanations and description pages.

As I was searching for each state, I was surprised at how outdated the sites were. So many required Internet Explorer, and a couple prevented me from even accessing the pages using Chrome.  And Pennsylvania… Netscape Navigator? Really? Well, it IS using classic ASP… so I guess it is to be expected.

I think I may do this for other commonly needed pages, too.  In one of the sessions, someone mentioned how difficult it was to find info about building permits… but since that gets into county and city pages, it may be beyond the scope of this project. I just need to figure out what pages are most commonly needed.

7 Useful Twitter Bootstrap Resources (updated)

Twitter Bootstrap is a CSS framework that I’ve been using for all my projects recently.  It makes getting a nice looking design quick and easy.  The simplicity of it has made it possible for all sorts of resources and “hacks” to pop up.  Here are some that I’ve found…

  1. Built With Bootstrap
    A directory of sites that use Twitter Bootstrap. Many of them are easily identifiable by the grey nav bar and button styles, but some have really extended it.
  2. jQuery UI Bootstrap
    Bootstrap (especially v2) has some nice javascript included, but if you like jQuery UI and want it themed like Bootstrap, this will do it.
  3. Darkstrap theme
    If you’d prefer to have things not so “bright”, this theme will make a dark version of Bootstrap. It’s actually a separate file, so it doesn’t alter the actual Bootstrap CSS file.
  4. Bootswatch
    As of today, these are 6 nice, premade themes using altered Bootstrap.
  5. Lavish
    This is a pretty neat project.  You add in the URL for an image and it generates a color scheme for Bootstrap based on the colors in the picture.  You can get some nice results.
  6. Fbootstrapp
    A Bootstrap theme that mimics the Facebook design. It’s also designed to work really well if you develop canvas apps that will actually be viewed ON the Facebook site… keeping the design consistent. It’s still using v1.
  7. The Bootstrap customizer
    Finally, the customization page from Bootstrap. Choose only the elements you want in your CSS, change the default values, and download.

 

UPDATE ( 3/7/2012)

8. Font Awesome
“iconic font designed for use with Twitter Bootstrap”

 

 

 

Making a box “width” keep that width, even with padding.

I’m saving this Paul Irish post for later (or always) use from now on. There’s nothing worse than defining something as 400px wide with 20px padding left and right and… oh wait, NOW it’s actually 440px wide and screwing up the layout.  So if I want the box to REALLY be 400px wide, I have to define it as 360px wide… *sigh*

So here’s the code:

/* apply a natural box layout model to all elements */
* { 
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box; 
}

Getting good with Linux (pt 2)

In part 1, I got Ubuntu installed and was able to connect from my Windows laptop.  Now I need to get Apache, MySQL, and PHP installed.

First, Apache.  The command to install is simple: sudo apt-get install apache2  … and a few minutes later, Apache is up and running. I put the ip address into my browser, and i got “It Works!”.  Sweet.

To get PHP working, I followed this installation guide.  The guide is more for if you are USING the machine you’re installing to, but it works over SSH.  The one extra thing I did was get into the /var/www directory… then type sudo nano test.php .  In the file I put in <?php phpinfo(); and then Ctrl-O to save it.  Then in the browser, went to http://192.168.1.6/test.php and I got the PHP configuration page.  It worked again!

I followed the install guide for MySQL, and in a couple of minutes it was done.  I kept the main user as “root” with no password, then the command “mysql -u root” got me connected with no problem.  Then there’s this handy page with lots of useful commands.

So I’m all set up.  Now, I’m wondering what to do next.  How can I transfer local files to the dev machine?  I’ve read about installing samba and then map a drive… or use git, and push it to the dev machine… or even something like Phing, which I’d need a crash course in. That’s next…