Laravel Large Project Series tutorials (vi) Optimization, unit testing, and deployment

Source: Internet
Author: User

This tutorial will cover error handling, usage of configuration files, unit tests, and deployment to Apache servers.

1. Error handling

If a user accesses a URL that does not exist or if there is an error in the server, we do not want to return a wrong page, but want to go back to a friendly prompt page, can be easily implemented in Laravel, Laravel has a very simple error and log processing, when there is an error on the server side, app/start/global.php There is an exception handler that handles all exceptions by default:

App::error(function(Exception $exception){    Log::error($exception);});

It writes the exception information to the log, and the log file defaults to app/storage/logs/laravel.log .

If you want to display a friendly error prompt page, we can create a view:

$ generate:view error

Modify error.blade.php :

@extends(‘_layouts.default‘)@section(‘main‘)    is an error!        return Index@stop

App::error(function(Exception $exception)added in:

return Response::view(‘error‘array500);

Now when the access error occurs, the error prompt page appears:

2.404 processing

When the URL of the visit does not exist, we can also return a friendly prompt page, first to create a view:

notFound

Modify notFound.blade.php :

@extends(‘_layouts.default‘)@section(‘main‘)    Sorrynot exist!        returnIndex@stop

app/start/global.phpadded in:

App::missing(function($exception){    return Response::view(‘notFound‘array404);});

Now when you visit a URL that doesn't exist, you'll see a page like this:

3. Configuration files

Sometimes we may need some pre-set values, program execution only need to refer to this value, such as page display when the number of pages per page, we can use the configuration file, in the Laravel to use the configuration file is also very convenient, we can app/config create a new name custom.php , To add:

returnarray(    ‘page_size‘10,);

Now you can use in the program, change to the paginate(10) paginate(Config::get(‘custom.page_size‘) line, where custom app/config the corresponding file name, corresponding to the page_size corresponding profile key name, configuration file can be based on whether you are the development environment or production environment for different configurations, detailed can view the official documents.

4. Unit Testing

Before the site is online, we usually need to do unit testing, Laravel provides a convenient unit test module. I only implement one example here, we can first app/tests create a MyTest.php file named, in which a MyTest class named, remember to inherit TestCase the class, and then you can write the test code:

 class MyTest extends TestCase {     Public  function testindex() {        $this->call (' GET ','/');$this->assertresponseok ();$this->assertviewhas (' articles ');$this->assertviewhas (' tags '); } Public  function testnotfound() {        $this->call (' GET ',' Test ');$this->assertresponsestatus (404); }}

After the test code has been written, we need to install a phpunit component that composer.json require-dev adds:

"phpunit/phpunit""3.7.*"

Then the composer update installation, after the completion vendor/bin/phpunit of execution, a little while the test results will appear, in our testing if you want to do some initialization operations, such as database migration and fill, etc., can be defined in the setUp method, remember to do first parent::setUp , if you want to restore the scene after the test is completed, Can be tearDown done in the method, if you want to use a specific configuration file at the time of testing, we can app/config/testing create it in the directory, it will automatically overwrite the original configuration when testing.

5. Deploy to Apache

After the test passes, we can deploy the website to the application server, in the production environment, we should app/config/app.php set the in debug false . This explains how to deploy to Apache servers. First of all, I declare the LAMP environment here is through tasksel installation, we first install mod_rewrite模块 :

sudo a2enmod rewrite

Then set the permissions of the /var/www directory to 777, this directory is the directory to store the site:

777 /var/www/

Then we copy the project folder we developed into this folder, here I am the blog folder:

$ cd /var/www/$ cp -r ~/laravel-project/blog/ .

The development project path above will be the same as yours, then we need to app/storage change the directory permissions to 777, because the storage folder will contain logs and so on, involving write operations:

$ cd blog/app/$ chmod -R777 storage/

Configure the server below:

$ sudo vim /etc/apache2/sites-enabled/000-default.conf 

To DocumentRoot/var/www/html change DocumentRoot /var/www/blog/public , and then modify apache2.conf :

sudo vim /etc/apache2/apache2.conf

Put

all

Added to

NoneRequireall granted

After that, start the Apache server now:

sudo service apache2 start

In the browser localhost , or 127.0.0.1 you can see our site, this deployment is complete.

6. Summary

This section of the tutorial on error handling optimization, configuration file usage, unit testing and how to deploy to Apache server, you can buy a domain name and a server, it is best to buy VPS Cloud server, virtual space is very limited, and then put your own written site to the server for everyone to visit.

The final code download:

$ https://github.com/shiyanlou/laravel-blog-6.git

This article is detailed from http://www.shiyanlou.com/courses/123, reproduced please indicate the source

Laravel Large Project Series tutorials (vi) Optimization, unit testing, and deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.