Book Image

Getting Started with Laravel 4

By : Raphaël Saunier
Book Image

Getting Started with Laravel 4

By: Raphaël Saunier

Overview of this book

<p>PHP powers many of the largest websites on the planet. Yet, even though it was specifically created for the Web, its shortcomings never cease to frustrate developers. This is where a tool like Laravel comes in. Rather than reinventing the wheel, Laravel reuses tried and tested components and principles and bundles them to form a cohesive whole and makes PHP development enjoyable again.</p> <p>Getting Started with Laravel 4 is a practical and concise introduction to the Laravel PHP framework. It covers its fundamental concepts and presents the many features that will boost your productivity when developing web applications. After introducing the key concepts and installing Composer, you will build a CRUD application and add more features to it in each successive chapter.</p> <p>This book introduces you to a different and more enjoyable way of writing PHP applications. You will start by learning about the key principles and the same development practices that Laravel encourages. Then, in subsequent chapters, you will create and successively add more features to a web application.</p> <p>You will learn how to use the arsenal of tools at your disposal and probably pick up some useful techniques along the way. Indeed, everything you will learn in this book is highly transferrable and applicable to other MVC frameworks. Laravel's routing mechanism, templating language, and object-relational mapper will have no more secrets for you. You will learn how to authenticate users, write tests, and create command line utilities that interact with your application with disconcerting ease. In addition to this, you will probably be surprised by the simplicity and expressiveness of your code.</p>
Table of Contents (15 chapters)
Getting Started with Laravel 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

String and text manipulation


The string manipulation functions are found in the Illuminate\Support namespace and are callable on the Str object.

Most of the functions also have shorter snake_case aliases. The Str::endsWith() method is, for example, identical to the global ends_with() function. We are free to use whichever one we prefer in our application.

Boolean functions

The following functions return true or false values:

  • The is method checks whether a value matches a pattern. The asterisk can be used as a wildcard character:

    Str::is('projects/*', 'projects/php/'); // Returns true
  • The contains method checks whether a string contains a given substring:

    Str::contains('Getting Started With Laravel', 'Python');
    // returns false
  • The startsWith and endsWith methods check whether a string starts or ends with one or more substrings:

    Str::startsWith('.gitignore', '.git'); // Returns true
    Str::endsWith('index.php', array('html', 'php')); // Returns true

Transformation functions

In some cases you need to transform a string before displaying it to the user or using it in a URL. Laravel provides the following helpers to achieve this:

  • This function generates a URL-friendly string:

    Str::slug("A/B testing's fun!");
    // Returns "ab-testings-fun"
  • This function generates a title where every word is capitalized:

    Str::title('getting started with laravel');
    // Returns "Getting Started With Laravel"
  • This function caps a string with an instance of a given character:

    Str::finish('/one/trailing/slash', '/');
    Str::finish('/one/trailing/slash/', '/');
    // Both will return "/one/trailing/slash/"
  • This function limits the number of characters in a string:

    Str::limit($value, $limit = 100, $end = '...')
  • This function limits the number of words in a string:

    Str::words($value, $words = 100, $end = '...')

Inflection functions

The following functions help you find out the plural or singular form of a word, even if it is irregular:

  • This function finds out the plural form of a word:

    Str::plural('fish');
    // Returns "fish"
  • This function finds out the singular form of a word:

    Str::singular('elves');
    // Returns "elf"