Category Archives: Laravel

You Can Now Contribute Web Development Content

Jafty Interactive Web Development has decided to start accepting user contributed blog posts! You can now submit any post that is related to out blog theme of web development and it will be published after admin moderation. As long as it is on topic and I think it improves the quality of my blog, it will probably be published. Some topics I would like to encourage users to submit posts about include:

  • PHP
  • JavaScript
  • HTML
  • XML
  • CSS
  • Search Engine Optimization/SEO
  • Freelancing as a web developer
  • Freelance writing
  • working from home
  • Laravel
  • WordPress
  • C Languages
  • Java
  • ASP.net
  • JQUERY
  • Ruby on rails
  • MySQL Database
  • SQL
  • PHP and MySQL
  • Remote server administration
  • Linux Command Line
  • Linux and Windows Servers
  • MAC and web development
  • PC vs MAC
  • Mobile Development
  • Mobile website optimization
  • PERL
  • AJAX
  • Adwords
  • CURL
  • Graphics
  • anything else related to web development!

That's just a list to fuel your mind. Feel free to submit any related topic. I will accept anything related to web development. I'm looking forward to some great blog posts! - Ian L.

Click Here to Sign Up and Begin Writing!

 

CodeLobster All-in-One Coding Software

CodeLobster PHP Edition Software Review

Written by Ian Lincicome of Jafty Interactive Web Development - Jafty.com

For the past couple of weeks, I have been evaluating CodeLobster's latest release of "CodeLobster PHP Edition" which I'll refer to as simply "CodeLobster" from here on out in this review. The current edition at the time of writing this review was CodeLobster PHP Edition Professional Version 5.4. I am reviewing CodeLobster because I am looking for a tool I can use for my web development business, Jafty Interactive Web Development. Also I was given a break on purchasing the pro version of CodeLobster in exchange for writing a product review on my site, Jafty.com. Please note that I am under no obligation to write a positive review. I will be as brutally honest as ever, I assure you. This review will include both the pros and cons of using CodeLobster rather than a one-sided review like many company's probably hire writers to write. In fact, if that was my intention, I wouldn't have mentioned that the someone had asked me to write this review. My ulterior motive for writing the review is to find the perfect program to do my work for my business. I am also evaluating other products as well because I want to work with the best.

I'm looking for an application that will allow me to seamlessly edit several types of code on several different servers without having to use a separate code editor, FTP program, web browser and Language reference material. To this date, I have been using three separate applications to accomplish my day-to-day work. I used FileZilla to copy files from a client's web server to my desktop. I used Notepad++ to edit the code files. Finally, before using FileZilla again to upload the files back to the client's server, I viewed the file in Firefox to see how it looks in a browser. That is if it is not a server-side language like PHP which I use the most. If I had to edit a PHP file, I had yet another couple of steps to perform because I'd have to upload the file to the server to view it and then repeat the whole entire process again if I had more changes to make to get it perfect. So I used FileZilla, Notepad++ and Firefox mostly up until now. I figure I could probably almost double my productivity with one good tool that does it all.

Technologies CodeLobster Supports

CodeLobster supports a wide range of technologies including:

  • PHP
  • HTML
  • JavaScript
  • CSS
  • SQL
  • MySQL
  • Version control systems such as SVN, Git etc.
  • CakePHP
  • CodeIgniter
  • WordPress
  • Drupal
  • Joomla
  • Facebook
  • jQuery
  • Smarty
  • Symfony
  • Yii

CodeLobster also has a very wide assortment of features including all the standard components you would expect in a code editor and many extras. I found a helpful built-in Help feature that also links to online help if you can't find what you need in the built-in documentation. The online documentation is surprisingly complete for a newer product.

Installing CodeLobster

The installation went smoothly and was very easy and comprehensive. Simply download and click the set up file and the installer will guide you through the simple set up process. Here is the Download Link: http://www.codelobster.com/download.html

When I first installed CodeLobster, it allowed me to select what languages/technologies I wanted to install. I only deselected two that I knew for sure I would most likely never use. I figured if there was even a small chance that I might use one of the items listed, then I should leave it checked. That way I will be prepared in case I need that particular technology in the future.

Pros and Cons

Once I began using CodeLobster I noticed both good and bad points regarding the program. I loved how versatile the program was right out of the box. I did not like the way the word wrap feature worked(View/Word Wrap). It breaks up words rather than splitting lines by the nearest space. I'd prefer it to not break up words at all or at least do so in a more logical manner(by syllables with hyphens for example). From what I understand from reading the forums and talking with my contact, CodeLobster plans to add an option between soft and hard word-wrap. Hard wrap is how it is now and soft wrap would be a word-wrap technique that doesn't break up words. I do hope they add it soon because it was one of the of the first things I noticed.

One of the first features I noticed and liked was the completely configurable hot-keys. You can configure any hot-key by going to Tools/Preferences/IDE/Hot keys. Another nice feature is the Find and Replace feature that is very much like that of Notepad++ including regular expression search and the ability to search and replace text in files, folders and sub-folders. This can be a huge time-saver for coders.

One of the first things I noticed and did not like was the lack of a decent spell checker. I know notepad++ has the option as a plugin, but not as a built-in feature. Either way would be great, but I think it should be a standard built-in feature. I realize that auto-complete helps with this, but not if you are writing a plain text file such as I am doing right now while writing this review. I talked to my contact at CodeLobster Software about this and he assured me that they plan to add a robust spell-checking feature in the near future. He mentioned that they plan on making several additions to the software and will be releasing future editions with new features and improvements ASAP. So, while CodeLobster may not have everything I would like it to have at the moment, I was assured that they are heading in the right direction with future expansion plans.

Another nice feature I also noticed right away was CodeLobster's tool bars. The tool bars are completely configurable and have a nice drag and drop feature to move tool bar items around as you please. The same can be said for the man different windows that can be added or removed from the work area.

CodeLobster is a code editor first & foremost and it does a wonderful job at editing PHP code which is my code of choice. If you are looking for a word processor, CodeLobster probably won't fit the bill, but it is a very robust code editor that can handle all of the languages I use for web development and several others.

CodeLobster's plugin ability is great to have in a code editing program. The pro version of CodeLobster comes with many plugins that the free version does not include from what I understand, so I think the pro version is definitely worth the cost for those of you who work with frameworks such as WordPress, Joomla, CakePHP, CodeIgniter, etc. I currently own the Pro version and I am happy with its performance so far and will be much happier once some of the additions are made in the near future.

Even the free version would be great for coders who use many different coding languages regularly but don't need the extra plugin abilities that require the pro version. As a full-time web developer, I mostly use PHP, HTML, CSS, JavaScript, WordPress and MySQL. I also occasionally use Joomla, XML, Perl, Python, Drupal, Smarty, Laravel, CakePHP and others, so I require the Pro version of CodeLobster myself. If you only use the most common languages such as PHP, HTML, CSS and JavaScript, you can probably do fine with the free version. Some of the things I require the Pro version for include WordPress support, Smarty, Laravel and CakePHP. Honestly there are to many features to list here. You should visit their web page at http://www.codelobster.com/details_phped.html to figure out which version is best for you. They also have a Lite version which you can read about on their site since I won't be covering CodeLobster Lite here.

As I continue to evaluate the software I am finding there are more and more features that I like. As a web developer, I need a tool that allows me to quickly edit code, test it and upload it to the client's server. So far, CodeLobster seems to be meeting my three main requirements quite well in most cases.

FTP Feature

CodeLobster's FTP ability is a very powerful feature. The FTP feature allows you to set up as many FTP connections as you need. I was able to copy all of my client's FTP connections from FileZilla into CodeLobster. Now I am able to click on a connection, edit a server file, preview it and upload it back to the server seamlessly from within a single application! This is huge for me because I used to use at least three different applications to do the same thing without CodeLobster. I can see that my work will get done a lot faster by using CodeLobster. In fact, I am already getting work done faster after only two days of using it.

The FTP feature is very functional, but for a guy like me who hates to read instructions, it took a little bit of fooling around using trial and error methods to get it set up for the first time with all of my client's 20+ FTP connections and configuring basic settings. Everything I needed to accomplish my FTP tasks is in CodeLobster, but the FTP system does need some improvements. For example it gave me trouble when trying to connect to a SFTP connection. It worked great with all non-secure FTP connections however. As luck would have it, out of over 20 current clients I work with, only one uses SFTP rather than FTP, so I am unable to test it on other secure SFTP servers to see if the problem is just with this one server or not. Unfortunately this is one of my regular clients so it is going to be a problem for me. It did connect using SFTP, but had issues when trying to edit files on the server which I was able to do fine with regular FTP connections. I did bring this issue to the attention of my CodeLobster contact however and he assures me it will be looked into ASAP.

Regarding the GUI set-up for FTP, in my personal opinion, the way the windows are organized in the work area could have been done a little better. For example, when I use FTP, I have to go to Tools/FTP to open the explorer window that lists FTP connections. That alone will allow you to open your FTP connection but won't allow you to see what's going on. If there was an error and the only window you had open in your work area was the explorer window, you wouldn't see that error. This is where I think it could have been done better. I would have programmed it so that when you open the FTP window, the output window opens along with it, right above it, similar to how FileZilla allows you to view console output. Of course this is just my opinion, it may not bother others and actually I am starting to get used to it so it doesn't bother me as much as time passes.

Search Features

The "Find in Files"(Search/Find in Files...) feature is great for finding text in open files. It could be improved to allow file search on the local directory tree files as well, but it is sufficient the way it is. Also I love the regular expression search as I mentioned earlier.

Also, I often use the incremental search feature when I edit code. While I am glad it has this feature, a small nuisance for me is that the incremental search box is not persistent across different views. I think it would be nice if the incremental search box would stay at the bottom of all view tabs once it is opened. I don't think it should close unless you manually close it. Again, this is just my preference, not a bug.

Nice-to-Have Features

One more thing I liked while test driving CodeLobster is that when editing .html and .css files I could hover over class names in the HTML or CSS code and it would show me the associated style declaration. Also when hovering over items in a .css file, it shows me information regarding the browser compatibility of the CSS code.

The HTML preview feature works very well. It is a built-in browser that shows the results of your code live without having to open a separate browser window. All you do is click on the "preview" tab. I did notice that sometimes you have to hit the refresh button to see your changes, but that is to be expected.

I also enjoyed the code formatting features very much. I tested them on an HTML file that I copied from an online web page. At first the HTML code was ran all together with no line breaks or indents. I simply went to the tool bar and clicked "Tools/Code Format/Format" and it formatted it perfectly. I am sure it will work with other supported languages equally well.

When you double-click on a variable name in the PHP editor, it highlights the variable and the dollar sign, so you can copy the whole thing and paste or search for it easily. Notepad++ wouldn't include the dollar sign and it was a bother to me.

CodeLobster has what they call a "Map" feature(View/Windows/Map) that I've never seen before. It is basically a small window with a tiny view of the document you're working on. It allows you to click anywhere in the Map to quickly jump to a section of the file that you need to work on. It is a great feature for working with large files.

Conclusion

I have concluded that CodeLobster will indeed fill a need in my current web development process. It is not yet developed enough to be the only tool I use simply because of that one SFTP connection it will not work correctly with. However I found that I can combine it with another tool, CyberDuck, as a work-around until the developers of CodeLobster work out the final kinks in the software and add some more nice-to-have type features such as spell-check and . In the end it still gets a thumbs up for all the wonderful features it has and for its robustness in general. Eventually I believe it will make the perfect all-in-one coding tool for my company. The other contender for my new all-in-one coding tool was CyberDuck and I can honestly say that CodeLobster has much more potential. CyberDuck is also in its early stages of development and it needs several improvements to be truly useful. CyberDuck fails in comparison to CodeLobster when it comes to Features. CodeLobster is truly made to be an all-in-one coding solution while CyberDuck is missing many of the necessary features to be a real competitor.

If you are interested in how I combined CodeLobster with CyberDuck to make up for CodeLobster's short-coming when connecting by SFTP, I'll quickly go over how to do it. Create a bookmark(that's what CyberDuck calls their FTP/SFTP connections) in CyberDuck to the SFTP server. CyberDuck allows you to set it up so you can use your own code editor with it in a way that allows you to click on a file from a remote server and open it in that editor. Then when you save it in that editor(CodeLobster), it saves it to the server. That way I still have a seamless process even though I have to combine two tools to get what I truly want.

Buy Scripts, Applications and Web Tools at Jafty.com/products

Yes, I finally am starting to sell some of my products that I have been developing for over 12 years now. I am only putting together the best tools I have made and remaking certain ones I find the most useful for sale on my website, Jafty.com/products

Types of Products for sale on Jafty.com/products

  • Email Marketing tools
  • Domain name tools
  • DNS tools
  • Server administration tools
  • webmaster tools
  • PHP tools
  • Customized tools for business
  • Have me build one custom for you if you don't see what you need

CLICK HERE TO VISIT Jafty.com/products/ and see what's listed for sale now. However, I do not have them all listed yet, so drop me an email or give me a phone call at 330-903-6074 or 330-417-3617 to find out how I can help you.

jafty0

Want to learn more about SEO? Check out these relative books from Amazon:

php composer dump-autoload returns error

When you open a route after doing work on your Laravel 4 application, you have to clear the autoload. If when you run:

php composer dump-autoload

...it returns error:

could not open input file: composer.

Then this is what I did to get around it:

  1. I was using xampp and localhost on Windows 7 so I opened a command prompt in Windows, not Xampp and went to the Laravel application directory with cd desktop/xampp/appname, then I found that php composer dump-autoload returns an error still, but the following command without php in front of it works: composer dump-autoload returns error
  2. Try opening your route again now and it should work.

I think the reason for this is the location of the php installation that composer uses, so that's your tip if you want to investigate further. For example instead of php, you may need  a complete path. Just guessing from something I remember reading folks, so don't hold me to this. If anyone wants to comment on it that would be helpful.

 

 

Cloning a Laravel Project

Today, I am making a copy of a Laravel project. I a doing it to have a testing and development environment that is separate from my main Laravel application so I can tinker with it without messing up the main project. Anyway, its a good idea, so here is how I did it on my localhost server. The process will be nearly the same on any server though.

  1. copy your main project folder and give it a new name so you can keep it in the same directory as your original project. By "project folder", I am referring to your entire Laravel project, so your project folder could be named anything (whatever you decided to call it), so it will be different for everyone. The main project folder should contain a "vendor" directory, an "app" directory, a "public" directory(unless you took it out and placed pubic files in the main project folder as I have done before) and there will be some files also, but I am just telling you basically what folders should be in a project to be clear on what I mean when I say we are cloning a project. For example, if you project directory was named "Project"  make copy that folder and place the copy in the same directory as Project and name it "Project2" or anything other than it's original name. I will refer to the original project folder as "Project" and the copy as "Project2" in this tutorial. If you are on the local server as I am, just right click on the Project folder and copy and paste it then rename it to "Project2". If you are on a live web server you will probably be using a command prompt and would do something like "cp Project Project2" from the command line while in the working directory that contains the project folder.
  2. Now add a new database for your cloned project. If the project you are cloning already has data in the database, then you will want to dump it to a sql dump file, otherwise just create a new empty database and give it a new name. I named mine. if you have a localhost set up like mine, you'd go to http://localhost/phpmyadmin/ and clone the database which I shouldn't have to explain here as it is something you should know if you are using Laravel. If you need help, Google Mysql/phpmyadmin tutorials along with the server type and you'll find what you need.

So far that did it. Just one main step? Yea, it looks like it(but then I quickly realized I would prefer to run the clone off of it's own database, so I added step 2 above), I tested it by going to http://localhost/Project2/ and it works, but if I run into problems later on, I will add notes to this tutorial on any further actions that may be required as I come across any. I may not because everything seems to work already!

Laravel Views Tutorial

This is a brief tutorial on how to create Views with Laravel 4. Views make up the "V" in the MVC or Model View Controller.

View File Formats

View files will have a .php file extension or a .blade.php file extension if you use the Blade templating engine which I'll explain later in this tutorial. All views live in app/views and sub-directories you can create in app/views.

A Basic View Example

A very basic view could consist of some HTML and a variable such as in the following example file we will name welcome.php and it goes in app/views/. Here is the syntax:

<html>
    <body>
        <h1>Welcome <?php echo $name; ?></h1>
    </body>
</html>

Passing Data to a View

You should notice the PHP echo statement in the view code. We can now display the view in a browser and  pass a name to the view's $name variable like this:

Route::get('welcome', function()
{
    return View::make('welcome', array('name' => 'Ian'));
});

Now when you visit example.com/yourapp/welcome, you will see a welcome message with the name you passed to your view in the route. That's a very basic view implementation. Next we will get a little more complicated.

Share Data with all Views

If you want a piece of data to be available to all views in your application, simply declare it in routes.php like this:

View::share('name', 'Adam');

Then anytime the name variable is called in a view with $name, the name you set, Adam in this case, will be displayed. This could be handy for a logged in user for example.

Views and Templates

Very often, you will want to use a template with a View and Laravel has incorporated the Blade Templating Engine just for that purpose.

BLADE Templates

The Blade Templating engine is driven by sections and template inheritance. All view files that use Blade will have the .blade.php file extension telling Laravel that the file is using the Blade template engine. Blade Views are also held in the app/views folder of your application and sub-directories of app/views.

Three Steps to Developing a Blade Template View

Step 1:

To help you understand both Sections and Inheritance, we will look at a Blade template view as an example. First, let's add a directory to app/views and call it layouts. So we now have app/views/layouts as a folder to keep our Blade layout templates. Next, create a new file called app/views/layouts/master.blade.php. This will be our master layout we may use for several pages, so it will be flexible. Copy the following lines of code into your master.blade.php file:

<html>
<body>
<nav>
@section('topnav')
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
@show
</nav>

<div>
@yield('content')
</div>

<div>
@section('sidebar')
Master sidebar content in all sidebars....<br />
@show
</div>

<div>
@section('footer')
Master footer content that gets overwritten so you won't see this in this example when viewing the "aview" page in a browser....<br />
@show
</div>

</body>
</html>

Step 2:

Now make another file and put this on directly in app/views and call it app/views/anypage.blade.php and add this:

@extends('layouts.master')
 
@section('content')
  <p>This will be overwrite</p>
@stop
 
@section('sidebar')
  @parent
   
  <a href="#">Section specific links will get appended...</a>
@stop

 

Step 3:

Now step 3 is to add our route to routes.php. Copy the following to the end of your routes.php file:

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

3 Steps Explained

  1. In step one we made our master.blade.php template file which is our master layout for several pages in our application. We used nav and div containers. nav could be a div, they act the same. You will want to add CSS style classes to these in your actual application. We left CSS out for simplicity of learning just what you need to know. It is assumed you know basic HTML, PHP and CSS if you are learning a template engine and a PHP framework. In master.blade.php we used a few different methods of displaying content for learning purposes. First we created a section with: @section('topnav') then we added some content that will show on all pages in the top nav. We closed the section with @show. @show is the common method for closing a section in a layout file. We will use something else for closing sections in views as you'll see below. Next we showed how to use yield to "yield" the content directly from the view, so a yield statement in a layout file is simply a placeholder for content which may be defined in our view as you'll see in step 2. After the yield example is another section example. This section, for the sidebar in our example, is designed to be static content that won't change in most pages, but could be overridden if necessary. FInally in our footer section we demonstrate content that is intended to be overwritten, but if it wasn't, you would see the default content that is there. You'll see how overwriting is done in the next step.
  2. Step 2 is where we define the view which extends the master template from step 1. Here you can see that first we tell Laravel that we are extending the master layout with extends('layouts.master'). Then we skip the nav because we are not changing anything there. That's why the first thing you see after extending the master layout is the content section that is declared with section('content') then followed by some HTML and ends with @stop. Notice the use of @stop in our view instead of @show like we used in our layout file to end a section. The main difference between @stop and @show is that @show yields the content immediately. They both end the session, but @show ends and shows the content at the same time. Next, you should notice the sidebar section has @parent in it. @parent indicates to Laravel's Blade that we intend to append to the content in the sidebar. Therefore whatever HTML is after @parent and before @stop will show up after the default content in the layout file from step 1. After that, I go on to demonstrate what happens by defualt in a section declared in a view without using @parent. WIthout using @parent, the default behavior of a section in a view when extending a layout is to overwrite the existing layout's content as you will see happen if you run the route in the next step, step 3.
  3. Finally in step 3, we declare our route in routes.php. This is simple and you've probably done it a hundred times by now if you are this far along in learning Laravel already, but just to be sure, all you do is include the file name of the file in our app/views folder from step 2 above without the .blade.php file extension in your make declaration when coding the route. In this case, we named our route "aview", so we get to the page by visiting example.com/appname/aview in a browser. Then the file name we put in the app/views folder was anypage.blade.php, so we used return View::make('anypage'); in our route next.

Summary

That's basically all there is to it. If you can practice and master those three steps you are well on your way to creating pages with Laravel and Blade templates. Good luck.

You can see all of my Laravel tutorials by CLICKING HERE.

 

Laravel Quickies

Laravel Quickies is a quick tutorial I developed to help you remember how to do the major coding steps for the Laravel MVC PHP framework. If you don't have an idea of how to do the things in this tutorial yet, you should probably read the more detailed tutorials I've written on each of the subjects below. This is meant just as a quick reference guide for those that already know the basics but need a quick refresher course in the high level steps to do things such as create a route, model, view or migration for example.

Laravel Quick Start Examples

Routes:

Enter the following code into routes.php to create a simple route:

Route::get('test', function(){

return('<h1>Laravel Route Example</h1>Ian L. of Jafty.com created this simple Laravel route test.');

});

visit the full Laravel routes tutorial here: http://jafty.com/blog/laravel-routes/

Controllers:

Add the following in a new file within the app/controllers directory to create a new controller:

class yourClass extends BaseController {

public function yourFunction() {

return "<h1>This is what gets output to the web page when the test route is visited by a user in this basic Controller Example</>";

}

The above controller would need the following route set up in routes.php to make it work:

Route::get('test', 'yourClass@yourFunction');

Finally, after you create a new controller, you always need to flush the autoload contents before the new controller will work, so do so by opening your command prompt that you use composer from and type:

composer dump-autoload

More Laravel quickies coming soon! For now, you can view all of my Laravel tutorials and posts by visiting the Laravel Category of my blog HERE!

Laravel Validation with Ardent Tutorial

If you are already looking into Laravel Validating, you should already know a bit about Laravel. So, you should be aware that Laravel is a MVC or Model View Controller PHP framework. Validation should occur in the Model part of the Model, View, Controller trio because the Model is where the business or database logic is kept. In this Laravel validation tutorial, we are going to discover how to do basic validation using the Ardent package.

Installing Ardent

The first thing we can do is install Ardent. To do this, we ned to open up the json file named composer.json which should be in your application root folder. We need to add the following code to the end of this file:
  "require": {
    "laravelbook/ardent": "dev-master"
  }
You wll want to add this near the bottom, but before the last closing bracket(}) so the last several lines of composer.json should look like this after adding the above code:

    "minimum-stability": "dev",

  "require": {
    "laravelbook/ardent": "dev-master"
  }

}

Save the composer.json file then open up a terminal window to your server and run the following command prompt command:

composer update

That will cause your application to update and upload the dependencies for Ardent since you required it in the composer.json file. The command may take a while to finish from the command line, so wait until you get your command prompt back so you know it is finished. You will see several removing, installing and downloading lines in the terminal window while it is working.

After the composer update command is finished, you may notice that several folders and files were updated in your application. Also it should have added a folder named laravelbook if you didn't already have it and inside that folder you will find the ardent directory. I mention this just so you can confirm it worked if you want to.

Validating with Ardent

Validating with Ardent is done in the Model as I mentioned above. Let's look at a very basic User.php model file as an example of how to use Ardent to validate input from the user.

Here's the default users.php file minus comments:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';
    protected $hidden = array('password');
    public function getAuthIdentifier()  {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function getReminderEmail() {
        return $this->email;
    }

}

You may have noticed, no validation occurs in the default file, but with Ardent, we can add some. Here is how to transform your users.php file to validate with Ardent:

  1. Add the following line of code towards the top of the file where you see the other two "use" declarations, so you can add the following just under where it says "use Illumincate....":
    use LaravelBook\Ardent\Ardent;
  2. Change the extends line just under the line we added in step 1 from:  class User extends Eloquent implements UserInterface, RemindableInterface {             to:
    class User extends Ardent implements UserInterface, RemindableInterface {
  3. Just before the } bracket that closes the User class at the bottom of the file add in the following  Ardent validation rules to make your model self-validating, meaning that you won't have to do anything, the model will simply reject invalid input and accept valid imput. So if a user trys to save something that doesn't pass validation, the save method will return false and nothing will be saved, otherwise if it validates, it will save the data and create the user in this case. Here is a validation rule set example to add to the end of your User class before the closing bracket:
     // Ardent validation rules:
    public static $rules = array(
      'email' => 'required|email',
      'password' => 'required|alpha_num|min:6|confirmed',
      'password_confirmation' => 'required|alpha_num|min:6',
    );
  4. Not all data needs to be saved, so we need to tell Laravel to purge the redundant data. In this case redundant data would be the password confirmation. Add the following line of code just below your validation rules to purge that data:
    public $autoPurgeRedundantAttributes = true;
  5. Save the User.php file then open your applications routes.php file in the main folder. Add the following route to test the validation changes you made to your User model:                                                     //test Ardent validation process:
    Route::get('/user_test', function() {
      $user = new User;
      $user->email = 'linian11@yahoo.com';
      $user->password = 'mypassword';
      $user->password_confirmation = 'mypassword';
      var_dump($user->save());
    });

That's all the steps, now go to the route you created in the final step above and if the user validates and is saved, you will see this in your browswer window only:

bool(true)

Then you know it worked. if you don't see that or see a false instead, go back and check your code.

Summary

I hope this Laravel Validation tutorial has helped you understand how to install and user Ardent so that you can make self-validating models from now on. You may want to expand on this validation technique to make it more user friendly, but this should at least get you pointed in the correct direction and get you started.

Starting with Laravel

Learning a new framework can be frustrating. Laravel is probably one of the more pleasant frameworks to learn, but if you are anything like me, you still dread taking on a new framework and you may be thinking, "Why can't I just use plain PHP?", well, you can! That's true. There is nothing stopping you, but PHP frameworks were designed to be time-savers and to make coding in PHP easier. Now, that said, there are exceptions to the rule as always. The main and most important exception being this:  You don't need a framework if you do not use PHP often enough. So ask yourself, "Do I use PHP daily or at least more than once a week?" if you answered yourself "yes" you may have some psychological issues, but none-the-less, by all means, pick and learn a good PHP framework and stick with it. No need to learn multiple PHP frameworks unless you are a real nerd, but one good PHP framework should suffice the average coder like myself. I have learned a  few out of necessity because certain clients want to use certain frameworks, so there are always reasons why you would learn more than one obviously. That brings us to exactly why I learned the Laravel framework. I did it because a client wanted me to use it, but I got to admit, it is probably the first PHP framework, I have tried out that I think I may actually keep on using during my career as a PHP developer. Okay, enough chit chat! Let's get started.

Makeing Laravel work in Root

Okay, I wasn't sure what to call this tutorial, I could of called it Laravel Pretty URLs or Getting Rid Of Public, but I didn't, so Making Laravel Work in Root will have to do and for SEO purposes, I already mentioned the other titles, ha ha. The gist of this tutorial is that it will teach you how to change ugly stock Laravel URLs that look like:

http://kingadvrt.com/laravel/public/test

TO look like this:

http://kingadvrt.com/test

The first example URL is what my first public Laravel installation produced. I had to navigate to the project name, laravel, then to the public folder and finally to the route I named in routes.php. Not very nice at all if you ask me or my client, in this case, kingadvrt.com. So, how do II make the king of advertising happy? Yes! Pretty URLs of course! Here is how it's done with Laravel:

Method 1:

you will have to edit your virtual host declaration in your server's config file. This is often in either httpd.conf or a file included in the httpd.conf file such as vhosts.conf or something similar. If you are not sure, search the include files and directories from your httpd.conf file which is normally in etc/httpd/conf/ directory or as in my case, I had a client with a public server that didn't allow access so I had to use a different solution. But if you are lucky enough to have a VPS or other server with httpd.conf access, simply edit the document root to that of your new public directory.

Method 2:

This may not be the preferred method, but it works if you would like to have a directory structure like:

http://kingadvrt.com/app/test

where everything is inside the app directory or whaever you want to call it. I am not referring to thte existing app directory in laravel, but a new one you can create to hold app, vendor, bootstrap, etc. So, what you do is upload all your Laravel files to a folder named app or whatever you want to call it that is inside of your web root directory so you have something like example.com/yourfolder to hold Laravel files. Then you move all the files from public down one level to yourfolder in this example. Then you have to edit the index.php file paths. Open index.php and replace any occurences of ../ with just / and you are done.

 

Installing Laravel Without Composer

Some people don't realize that Composer isn't required to use Laravel. It is nice to have however and should be used when ever possible. If you have no reason for excluding Composer, see my original tutorial on Installing Laravel with Composer HERE. For those of you that cannot use composer because your hosting provider doesn't allow it or because you want only use composer on your primary developing environment, this article is for you. In this article, I am using Xampp, but the basics will apply to any basic web server.

Setting Up a Basic Working Environment For Laravel

If you are just experimenting or need a secondary development environment, then I suggest cloning your original Xampp server and installing Laravel there. Follow my tutorial on Cloning Xampp by CLICKING HERE to learn how to clone Xampp to have two working copies of your development environment for testing and developing more applications. If you already have a copy of Xampp or another web server working where you want to install Laravel, then don't worry about this part of the tutorial. It is over now anyway. Ha ha.

Download Laravel

If you have downloaded Laravel before, you can use those files, otherwise download a fresh copy BY CLICKING HERE to get it from github.com or just go to http://laravel.com/. If you go to Laravel.com, click on the five minute quick start button and you will see that they instruct you to use composer, but under that they provide a link to download from github which is what you want to do in this case. I provided both links because Laravel.com has general information that is useful for beginners, but you could just download from github if you are in a hurry and just want to follow this tutorial to get up and running.

Here's the Gotcha!

You have to run composer once! I know, I said we were installing Laravel without composer, but what I meant was for cases where you cannot install composer like production shared server environments. This is basically a work-around for those that can not install Composer on a client's server or on their own. You can however almost always install Composer on your Local Machine. I see no reason why anyone couldn't! So go ahead and run composer. After you do so on your local machine, you will have all the dependencies necessary downloaded into your Laravel installation and you can now move it to a server that can not or does not, for whatever reason, use Composer. Now, if you have already installed Laravel on your original Xampp server and are working on a clone now, you can move a project over without composer just by moving the Laravel directories over to the htdocs folder of your new server, so see, we are installing Laravel without Composer after all! I told you so!. The "Gotcha" is that you do have to run Composer somewhere at least one time to get all the dependencies required to run Laravel. So, if you can't do it on the production server, do it on your localhost and upload the entire working Laravel installation to your production machine when it's ready. I'll explain just how to do this later, so keep reading!

Running Composer from Localhost

So, as I mentioned, you all should be able to run Composer from your localhost, so go ahead and do that using your Xampp installation by opening up a terminal window from your Xampp control panel. Just go to the file, xampp-comtrol in your main Xampp folder and fire up your server if you have not already, then click on the "Shell" button to get a shell prompt.

Okay, before we install our Laravel dependencies, make sure you have yourself in order...or at least have your Laravel directory in order. I can't help you with you ha ha. So, if you installed Laravel from github.com as I suggested above, unzip the laravel-master.zip folder into your htdocs directory of xampp. Sadly it will be in a sub-folder like laravel-master/laravel-master and we probably don't want that, so just make a new folder in htdocs called "laravel" or whatever you want to call your project and open up the final laravel-master folder, the one with all the files and cut all the contents and paste them into your final project directory.

Now go back to your shell and type:

composer install

Type the above from the shell prompt to have Composer download the necessary dependencies and add them to your Laravel project. You will notice a new folder in your Laravel directory called "vendor" that is the results of running the composer install command, but be patient, depending on your internet service provider, it could take several minutes to complete. Watch your shell prompt, when it is done, it will let you know by returning your command prompt to you.

Test Laravel

To see if it worked, go to:

http://localhost/laravel/public/

If you named your project "laravel" as I suggested above, then that URL should now work, but if you named it something else, replace laravel with whatever you named your Laravel folder. If your efforts were a success, you should see a page like in the following screenshot I took while making this tutorial:

Snap 2013-10-08 at 16.07.01All is god if you see the above on your screen navigating to your Laravel public folder. If you see anything else, you have issues I suspect. Read on for help! There is a troubleshooting section below, trust me. It may be a ways down the page, but it is there, I promise.

 

Moving Laravel from localhost to a Live Web Server

Here comes the fun part! This is where you folks that don't want to or can't use Composer on your production server will thank me. This part was tricky for me the first time because I couldn't find a tutorial on it anywhere, which also in part inspired me to write out, so hopefully if you are reading this, you won't have the difficult time I did. Well, if you are the thanking type anyway. If not, don't worry about it, my feelings won't be hurt. I don't know you anyway. So, go to your nasty old public shared host and make a FTP connection to it. I like to use Filezilla for this. If you don't know how to connect to your web server with FTP, contact your host or Google it and get instructions. Most hosting support departments are prepared to answer such elementary questions as how to access your account using FTP, so don't be afraid to ask! Once you have established an FTP connection, make a new directory for your Laravel Project or upload the existing project from your local machine and use that. Here is all you are going to need to do to move your Laravel project from localhost to another web server:

  1. Go to your project on your local machine. If you don't have one and need one just for testing, create a simple route at least so you have something in your project to migrate to a new server for learning purposes. CLICK HERE to see my simple Laravel route tutorial to create a route if you don't already know how.

 

Troubleshooting, Help Me!

Here are some possible issues you may have when following this tutorial if you didn't follow every small detail to the letter:

  • Laravel may be configured wrong for your environment. When I wrote this tutorial, I assumed you were working on your localhost and that you used xampp installed on your local machine. That also assumes that your working directory is going to have http://localhost/ as it's root. If that isn't the case, you will need to set some configuration settings in your Laravel installation. Look in your app/config directory for clues as to how to resolve such issues. Also your database may need configured. Laravel, conveniently for some of us, comes set up for a local host install with default settings for Mysql which uses root user and no password by default out of the box, but if you have changed this, you will have to go into your database..php file and make changes. It is located in app/config/database.php, so go there and make the appropriate changes if necessary and try to start your project again. It should work now.
  • Apparently my directory paths are different and are causing issues when I moved my installation to a live web server! You can configure Laravel directory paths in  bootstrap/paths.php.
  • Where can I find more help, your tutorial sucks and I can't get my Laravel app working! Okay okay, jeez! give me a break here... go to laravel.com/docs and happy hunting. No need to be rude.

 

 

 

Learning Laravel Models

Laravel models are based on the common MVC or Model-View-Controller principles. If you don't know what MVC is at all, you can Learn More about MVC Here because I just want to get to the meat of learning how to use Laravel Models in this Laravel models tutorial.

Prerequisites

You may have learned from following my Laravel tutorials, that you often need parts of other Laravel tutorials to fully understand the current tutorial. Well guess what? this applies here as well. You should have an understanding of the following areas of Laravel before attempting to learn about models:

The User Model

Since the user model is necessary in almost any modern web application, Laravel has created a basic user model and provided it in  app/models/User.php  for you to manipulate and copy as needed.

Generally models will follow the same naming convention as the example User Model. So if you added a new model to your project called Cars that was a representation of cars owned by the site's users then that model would also go in the app/models directory and would be named Cars.php so your complete path would of course be app/models/Cars.php.

Models and the Database

Each model in your project is directly related to a table in the database. By default that table will take the same name as the model, but  for the User model the table name is users, so for Cars, the table name would be cars. Follow that basic pattern and you should be okay. If you want to use a different name you can declare the table name for your model as you will see in the example code below.

Since every model interacts with a database table, the first thing we need to do is make our model class extend the Eloquent class. The Eloquent class is a database class Laravel uses with every model in order to interact with the database. So your first line of actual code should read:

class User extends Eloquent {}
Now we have started our class. Simply replace "User" with your class name if you are working on a different class.
To get an idea of how a model class should be written, you can follow the default User model that comes shipped with Laravel. Also while opening the class statement, the User model implements a couple interfaces:
implements UserInterface, RemindableInterface
so the complete first line of the User model looks like:
class User extends Eloquent implements UserInterface, RemindableInterface {

After that, as I mentioned above, is where you could change your table name, however since the User model doesn't do so, the following line could have been left out. It was most likely left in as a way to demonstrate how to name a table other than the default one for those who have a need or desire to do so. Here is how the User model declares its table name:

protected $table = 'users';
As I mentioned, since the name is users and that would be automatically assumed since the name of the model is User, this line is not mandatory but does show you what to do if you wanted to use a table named something other than users which you could do if you wanted simply by replacing "users" with "table_name" for example.
The next line in Users.php looks like:
protected $hidden = array('password');
What the above line does is prevents the user's password from being displayed when you might not want it to such as in a json API call. There are a couple of other measures that could be applied here to further protect your data such as the $fillable and the $guarded properties. $fillable tells Laravel which columns of your database table can be filled and guarded tells Laravel which ones cannot be filled basically. Here is an example of each:
protected $fillable = array('username', 'email');
protected $guarded = array('id', 'password');
The first line above tells us that username and email are allowed to be filled in the database by the user and the second line tells us that they cannot fill in the id and password fields.
Returning Data from a Model
The next practical thing the User.php Model allows us to do is return some data from the database with the following three functions:
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }
    public function getAuthPassword()
    {
        return $this->password;
    }
    public function getReminderEmail()
    {
        return $this->email;
    }
That is all for the User class actually, so lets explain the last three functions. The first is for returning a unique identifier of the user. The second returns the user's password and the third returns that user's reminder email address. Pretty simple really!

How to Use a Model

Everything you have learned so far is great, but how about seeing it in action? Yes, let's do that! In order to use the model, we need to define a route in the app/routes.php file, so open that file and append the following code to the end:
If the code worked, you will see:
bool(true)
only in the resulting page that opens. If it fails you will probably see a big long error page. in which case you will have to go back and check each step of this tutorial to see what you missed or make sure you set up the database and users table properly. If you have not done, so first, you need to create the users table by following this tutorial about database tables and Laravel:
When you are done and have successfully added a user to the users table, comment out the code in routes.php so you don't recreate the user every time you or someone else lands on that url.

Summary

To learn more about Laravel, follow more tutorials I have written by CLICKING HERE!

Creating Database Tables with Laravel

Okay, today we are going to cover how to get started with Laravel Database tables. You can create databases with Laravel, but for this tutorial on creating tables with Laravel, we are going to simply use phpmyadmin to make our database if you don't already have one made for your Laravel project.

Prerequisites

If you have not yet even installed Laravel, see my first tutorial on Laravel, Installing Laravel on XAMPP at http://jafty.com/blog/installing-laravel-php-framework-on-xampp/

That's really all you have to do before using this getting started tutorial, but you may need to reference other tutorials as you go depending on what you already know. I will try to reference the proper ones throughout this tutorial series to help you learn.

Selecting a Database for Laravel

As you probably are aware, most web sites need a database, so the first logical step to getting your Laravel project off the ground after installing the Laravel PHP framework itself, is to add a database. I am going to instruct you on how to install a MySQL database with Laravel. If you want to use a different database or storage option..... well, you are on your own. Sorry, but I don't know why you would want to use anything other than the most popular free developer's database available, MySQL. How do I know? You may have a legitimate reason, unfortunately, I just can't help you, but Laravel can, they have an abstraction layer that makes the syntax for all database and storage methods the exact same. No matter what database you use, you will be okay, but some of the steps in this particular tutorial will be slightly different, but don't worry too much about that, they should be obvious if you have enough experience with your database of choice which you should if you are ready to jump into learning a whole new PHP framework.

Configuring a Laravel Database

Now you are ready to make changes to the config files for Laravel to accommodate your database. Find the config file at app/config/database.php. If you are using a localhost install of Laravel for learning purposes, which is probably smart, then your config file changes will be minimal really. All you have to do is change the name of the database in the "database" variable at or about line number 57(depending on how many edits you made and what version of Laravel you are using). Change the default value of 'database' to the name of your database(unless you named your database, 'database'). I named my database 'laravel_test' for this tutorial. The database will be the main database for your project and will hold all of the project's tables.

Create a Laravel Database

Now you have to go to your PHPMyAdmin url and create the actual database. If you are developing on the localhost, you can go to localhost/phpmyadmin or wherever you installed phpmyadmin and create a database named after the same name you set in your config file for the 'database' option in the database.php config file. In my case, I named my database 'laravel_test' and created that same database from localhost/phpmyadmin.

Creating a Database Table in Laravel

Okay, now that you have created a database, it's time to really get to the meat of this tutorial and execute some code in Laravel to make a table on your new database.

Okay, remember I promised to tell you if you might need another tutorial to understand a part of this one? Here is the first case this has come up. You may wish to take a break from this tutorial now and check out my tutorial on Laravel Routes at:

http://jafty.com/blog/laravel-routes/

...or you may want to assume you can figure them out from the example in this tutorial, but you won't get the whole idea of routes from this tutorial as it only uses routes to make a database table and that is not what they are really all about. Routes are actual routes to your different parts of your Laravel project, hence the name, "routes".

So, whether or not you decided to take my advice and visit my "routes" tutorial, we are moving on! Here is the code you need to add to routes.php in order to create a database table using Laravel. as you can see, it's a lot simpler than creating a table in .sql and a bit faster than creating one in phpmyadmin if you use it correctly:

Route::get('make_table', function()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 60);
$table->timestamps();
});
});

If you have already learned about routes, you know from the above code to go to:

http://localhost/laravel/project1/public/make_table

in my case to execute the above code. Your path will depend on how you installed Laravel of course, but it should be similar if you are following my tutorials. You won't see anything but a blank window open when going to the URL. To check your work, go to phpmyadmin and examine the users table inside the database you created. It should have all the fields from the code above, but note that the timestamps() function actually creates two columns in the table, created_at and updated_at. So you should have six fields in your table, not five as you may have expected with only five lines of code.

Different Types of Data Fields

As you may know there are several types of data fields in Mysql, such as varchar, increment, bigint, etc just to name a few. Here is a list of Laravel commands for each so you can create the necessary fields of data for your own table:

Command Description
$table->increments('id'); Incrementing ID to the table (primary key).
$table->bigIncrements('id'); Incrementing ID using a "big integer" equivalent.
$table->string('email'); VARCHAR equivalent column
$table->string('name', 100); VARCHAR equivalent with a length
$table->integer('votes'); INTEGER equivalent to the table
$table->bigInteger('votes'); BIGINT equivalent to the table
$table->smallInteger('votes'); SMALLINT equivalent to the table
$table->float('amount'); FLOAT equivalent to the table
$table->decimal('amount', 5, 2); DECIMAL equivalent with a precision and scale
$table->boolean('confirmed'); BOOLEAN equivalent to the table
$table->date('created_at'); DATE equivalent to the table
$table->dateTime('created_at'); DATETIME equivalent to the table
$table->time('sunrise'); TIME equivalent to the table
$table->timestamp('added_on'); TIMESTAMP equivalent to the table
$table->timestamps(); Adds created_at and updated_at columns
$table->softDeletes(); Adds deleted_at column for soft deletes
$table->text('description'); TEXT equivalent to the table
$table->binary('data'); BLOB equivalent to the table
$table->enum('choices', array('foo', 'bar')); ENUM equivalent to the table
->nullable() Designate that the column allows NULL values
->default($value) Declare a default value for a column
->unsigned() Set INTEGER to UNSIGNED

Summary

That is it, you know now how to create a table using Laravel and routes! congratulations!

To learn more, see all my Laravel tutorials by Clicking HERE!

 

 

 

laravellogo

Installing Laravel PHP Framework on XAMPP

This is a straight-to-the-point tutorial on how to install and use the Laravel PHP framework with a local XAMPP server on your machine's localhost. This post covers installing Laravel, but be sure to read other posts here in the Laravel Category to learn much more about the Laravel PHP framework.

Prerequisites:

  1. php version 5.3.7 or higher
  2. mcrypt module which comes with xampp and wampp by default so you shouldn't have to do anything, but maybe check php info to make sure it's there.
  3. Make sure openssl is enabled in your php.ini file
  4. make sure your httpd.conf file includes the httpd-vhosts.conf file in it.

Install Directions

  1. First, install composer by going to http://getcomposer.org/download/ and clicking on the link that reads: "Composer-Setup.exe" just under the "Windows INstaller" heading on that webpage.
  2. Secondly, install Laravel by going to http://laravel.com/ and click on the "Five Minute Quickstart" button to get a full set of instructions in case you need help, but I'll give you the basic install steps next to keep it quick and easy for you.
  3. Go into your Xampp directory, open the htdocs folder and make a new directory for your Laravel project. You can name it simply "laravel" if you like to keep it easy to follow these instructions.
  4. Open your Command Prompt in Windows by clicking on start/All Programs/Accessories/Command Prompt and change your current working directory to that of the laravel folder you just created by using the cd command followed by the directory path.
  5. copy and paste the following text into your command prompt window: composer create-project laravel/laravel your-project-name --prefer-dist
  6. before you hit enter, replace "your-project-name" with a project name of your choice such as "project1" as I did for this tutorial. Then press enter and Laravel will take several minutes to install usually.

That is all there is to a simple install on Xampp. Now you can go to your favorite browser and open up http://localhost/laravel/project1 and you should see the Laravel logo along with a quick message saying "You have arrived." Now it's time to learn how to use Laravel and find out about some of Laravel's features.

Bundles

Bundles are add-ons that you can download and plug into your Laravel installation for added functionality. Be sure to check out the bundles at http://bundles.laravel.com/

Click on the Laravel category at the bottom of this page or type "laravel" in the search box of my blog to find more tutorials on Laravel right here! ...or CLICK RIGHT HERE to jump there now.

Laravel Routes & Controllers

One of the first things I learned when discovering the Laravel PHP framework, was how to use routes. Routes are a simple method of using sub-directories in a URL to show different content. For example you can set a route to "about" and code that route to show your about page content. Let's expand on that idea with a code example to help you get it:

Go to your project directory in Laravel and locate the app directory and the file name routes.php. This is where you can add your own routes. Lets start with simple route by adding the following code to routes.php at the end of the file:

Route::get('about', function(){

return('<h1>About Us</h1>Jafty.com is a fully functional web development company offering almost any service to do with web development, including, but by no means limited to, PHP, HTML5, CSS3, JavaScript, Photoshop, Logo Design, Jquery, Server Installation, Site Migration, WordPress, Joomla, Linux servers, Windows servers, etc. Just ask, I pretty much do it all. Yes, Jafty.com is basically, jut me, Ian L. I do hire additional help as needed ,but I personally manage and oversee every project that comes through my company and make sure that it meets or exceeds my rigid quality control standards.');

});

You can copy and paste the above green text into your app/routes.php file at the end and it will produce the following when you go to (in my case)localhost/laravel/project1/public/about. Here is a screenshot:

Snap 2013-09-19 at 12.13.15Now you may not wish to put all that code inside your routes.php file, especially in times where you need a lot more code for a page. You can use controller classes with your routes to effectively put most of the code in another file which I will explain next.

Here is an example of a basic controller class:

class UserController extends BaseController {

    public function showWelcome()
    {
        return 'Hello controller world!';
    }

}

to make that class work, you would put the above code in a file named "UserController.php" inside of the app/controllers folder of your Laravel project directory and then add the following to your app/routes.php file at the end:

Route::get('usercontrol', 'UserController@showWelcome');

Finally, after you create a new controller, you always need to flush the autoload contents before the new controller will work, so do so by opening your command prompt that you use composer from, change the working directory to that of your application and type:

composer dump-autoload

I've found you don't need to do the above command if you only create a route, but if you make a controller, do it! Also if you have issues running the above command from localhost, make sure you have the correct path and command prompt. If you installed Composer through Windows, open a Windows command prompt. If you installed it in Xampp, open a Xampp command prompt etc.

That's all there is to learning how to use basic routes and basic controllers. Of course there are more complex solutions than this, but this will get you started off on the right foot at least. To learn more, CLICK HERE FOR MORE LARAVEL TUTORIALS. I have written a boat load of Laravel tutorials on this blog and am adding more daily as of September of 2013, so check back if it is still 2013 when you read this or the start of 2014 even.