Home Backend Development PHP Tutorial Laravel Large Project Tutorial Series (6) Optimization, Unit Testing and Deployment

Laravel Large Project Tutorial Series (6) Optimization, Unit Testing and Deployment

Aug 08, 2016 am 09:29 AM
apache app error exception laravel

This tutorial will explain error handling, use of configuration files, unit testing and deployment to Apache server.

1. Error handling

If the URL visited by the user does not exist or there is an error in the server, we do not want to return an error page, but want to return a friendly prompt page, which can be easily implemented in Laravel, Laravel There is very simple error and log processing. When there is an error on the server side, there is an exception handler in app/start/global.php by default to handle all exceptions:

<code><span>App</span><span>::error(function(Exception</span> $<span>exception</span>)
<span>{
    <span><span>Log</span>:<span>:<span>error($exception)</span></span></span>;
<span>}</span></span>);</code>
Copy after login

It will write the exception information to the log, The default log file is app/storage/logs/laravel.log.

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

<code><span>$ </span>php artisan <span>generate:</span>view error</code>
Copy after login

Modify error.blade.php:

<code><span>@extends</span>(<span>'_layouts.default'</span>)

<span>@section</span>(<span>'main'</span>)
    Sorry, there <span>is</span> an error!
        <span>return</span> Index

<span>@stop</span></code>
Copy after login

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

<code><span>return</span> Response<span>::view</span>(<span>'error'</span>, <span>array</span>(), <span>500</span>);</code>
Copy after login

Now when an access error occurs, an error prompt page will appear:

2.404 processing

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

<code>$ php artisan generate:view <span>not</span><span>Found</span></code>
Copy after login

Modify notFound.blade.php:

<code><span>@extends</span>(<span>'_layouts.default'</span>)

<span>@section</span>(<span>'main'</span>)

    <span>Sorry</span>, the page you requested does <span>not</span> exist!
        <span>return</span><span>Index</span><span>@stop</span></code>
Copy after login

Add in app/start/global.php:

<code>App::missing(<span><span>function</span><span>(<span>$exception</span>)</span>
{</span><span>return</span> Response::view(<span>'notFound'</span>, <span>array</span>(), <span>404</span>);
});</code>
Copy after login

Now when the URL you visit does not exist, the following page will appear:

3. Configuration file

Sometimes we may need some values ​​that have been set in advance. When the program is executed, we only need to reference this value, such as the number displayed on each page when displaying in paging. We can use the configuration file, in Laravel It is also very convenient to use configuration files in app/config. We can create a new one named custom.php and add:

<code><span>return</span><span>array</span>(
    <span>'page_size'</span> => <span>10</span>,
);</code>
Copy after login

Now you can use it in the program, put paginate(10) Just change it to paginate(Config::get('custom.page_size'), where custom corresponds to the file name under app/config, page_size corresponds to the key name in the corresponding configuration file, and the configuration file also You can configure different configurations according to whether you are a development environment or a production environment. For details, you can view the official documentation.

4. Unit testing

Before the website goes online, we usually need to perform unit testing. Laravel provides a very convenient unit testing module. . I only implement an example here. We can first create a file named MyTest.php under app/tests and define a class named MyTest in it. Remember to inherit the TestCase class. Then you can write the test code:

<code><span><span>class</span><span>MyTest</span><span>extends</span><span>TestCase</span> {</span><span>public</span><span><span>function</span><span>testIndex</span><span>()</span>
    {</span><span>$this</span>->call(<span>'GET'</span>, <span>'/'</span>);
        <span>$this</span>->assertResponseOk();
        <span>$this</span>->assertViewHas(<span>'articles'</span>);
        <span>$this</span>->assertViewHas(<span>'tags'</span>);
    }

    <span>public</span><span><span>function</span><span>testNotFound</span><span>()</span>
    {</span><span>$this</span>->call(<span>'GET'</span>, <span>'test'</span>);
        <span>$this</span>->assertResponseStatus(<span>404</span>);
    }
}</code>
Copy after login

After the test code is written, we need to install a phpunit component, add it to require-dev in composer.json:

<code><span>"phpunit/phpunit"</span>: <span>"3.7.*"</span></code>
Copy after login

and then composer updateinstall it , execute vendor/bin/phpunit after completion, and the test results will appear after a while. If we want to do some initialization operations during our test, such as database migration and filling, etc., we can define it in the setUp method. , remember to execute parent::setUp first. If you want to restore the scene after the test is completed, you can do it in the tearDown method. If you want to use a specific configuration file during testing, we can do it in app/config Created in the /testing directory, it will automatically overwrite the original configuration during testing.

5. Deploy to Apache

After passing the test, we can deploy the website to the application server. In the production environment, we should set debug in app/config/app.php to false . Here we explain how to deploy to the Apache server. First of all, let me declare that the LAMP environment here is installed through tasksel. We first install the mod_rewrite module:

<code>$ <span>sudo</span> a2enmod rewrite</code>
Copy after login

and then set the permissions of the /var/www directory to 777. This directory is the directory where the website is stored. :

<code>$ sudo chmod -R <span>777</span> /<span>var</span><span>/www/</span></code>
Copy after login

Then copy the project folder we developed into this folder. Here is the blogfolder:

<code><span>$ </span>cd /var/www/
<span>$ </span>cp -r ~<span>/laravel-project/blog</span><span>/ .</span></code>
Copy after login

The above development project path must be the same as your own, and then we need to put app/ Change the permissions of the storage directory to 777, because the storage folder will store logs, etc., involving write operations:

<code><span>$ </span>cd blog/app/
<span>$ </span>chmod -<span>R</span><span>777</span> storage/</code>
Copy after login

Configure the server below:

<code>$ sudo vim /etc/apache2/sites-enabled/<span>000</span>-<span><span>default</span>.conf </span></code>
Copy after login

Change DocumentRoot/var/www/html to DocumentRoot /var/www/blog/public, then modify apache2.conf:

<code>$ <span>sudo</span> vim /etc/apache2/apache2.conf</code>
Copy after login

Add

<code>AllowOverride <span>all</span></code>
Copy after login

to

<code>Options Indexes FollowSymLinks
AllowOverride <span>None</span><span>Require</span><span>all</span> granted</code>
Copy after login

, now start the Apache server:

<code>$ <span>sudo</span> service apache2 start</code>
Copy after login

Visit localhost or in the browser 127.0.0.1You can see our website, and the deployment is complete.

6. Summary

本节教程讲了错误处理优化、配置文件的使用、单元测试以及怎么部署到Apache服务器,你可以买一个域名和一个服务器,最好买VPS云服务器,虚拟空间非常有局限性,然后把你自己写的网站部署到服务器让大家一起访问。

最后的代码下载:

<code><span>$ </span>git clone <span>https:</span>/<span>/github.com/shiyanlou</span><span>/laravel-blog-6.git</span></code>
Copy after login

本文详细出自http://www.shiyanlou.com/courses/123,转载请注明出处

以上就介绍了laravel大型项目系列教程(六)之优化、单元测试以及部署,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
The difference between laravel and thinkphp The difference between laravel and thinkphp Apr 18, 2025 pm 01:09 PM

Laravel and ThinkPHP are both popular PHP frameworks and have their own advantages and disadvantages in development. This article will compare the two in depth, highlighting their architecture, features, and performance differences to help developers make informed choices based on their specific project needs.

Laravel vs. Python (with Frameworks): A Comparative Analysis Laravel vs. Python (with Frameworks): A Comparative Analysis Apr 21, 2025 am 12:15 AM

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Laravel framework skills sharing Laravel framework skills sharing Apr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

Laravel user login function list Laravel user login function list Apr 18, 2025 pm 01:06 PM

Building user login capabilities in Laravel is a crucial task and this article will provide a comprehensive overview covering every critical step from user registration to login verification. We will dive into the power of Laravel’s built-in verification capabilities and guide you through customizing and extending the login process to suit specific needs. By following these step-by-step instructions, you can create a secure and reliable login system that provides a seamless access experience for users of your Laravel application.

What are the differences between yi2 and tp5 What are the differences between yi2 and tp5 Apr 18, 2025 pm 11:06 PM

With the continuous development of PHP framework technology, Yi2 and TP5 have attracted much attention as the two mainstream frameworks. They are all known for their outstanding performance, rich functionality and robustness, but they have some differences and advantages and disadvantages. Understanding these differences is crucial for developers to choose frameworks.

NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

Laravel: MVC Architecture and Best Practices Laravel: MVC Architecture and Best Practices Apr 19, 2025 am 12:13 AM

Laravel's MVC architecture improves the structure and maintainability of the code through models, views, and controllers for separation of data logic, presentation and business processing. 1) The model processes data, 2) The view is responsible for display, 3) The controller processes user input and business logic. This architecture allows developers to focus on business logic and avoid falling into the quagmire of code.

Frontend with Laravel: Exploring the Possibilities Frontend with Laravel: Exploring the Possibilities Apr 20, 2025 am 12:19 AM

Laravel can be used for front-end development. 1) Use the Blade template engine to generate HTML. 2) Integrate Vite to manage front-end resources. 3) Build SPA, PWA or static website. 4) Combine routing, middleware and EloquentORM to create a complete web application.

See all articles