


Laravel Large Project Tutorial Series (6) Optimization, Unit Testing and Deployment
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
and then composer update
install 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>
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>
Then copy the project folder we developed into this folder. Here is the blog
folder:
<code><span>$ </span>cd /var/www/ <span>$ </span>cp -r ~<span>/laravel-project/blog</span><span>/ .</span></code>
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>
Configure the server below:
<code>$ sudo vim /etc/apache2/sites-enabled/<span>000</span>-<span><span>default</span>.conf </span></code>
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>
Add
<code>AllowOverride <span>all</span></code>
to
<code>Options Indexes FollowSymLinks AllowOverride <span>None</span><span>Require</span><span>all</span> granted</code>
, now start the Apache server:
<code>$ <span>sudo</span> service apache2 start</code>
Visit localhost
or in the browser 127.0.0.1
You 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>
本文详细出自http://www.shiyanlou.com/courses/123,转载请注明出处
以上就介绍了laravel大型项目系列教程(六)之优化、单元测试以及部署,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

There are many ways to solve CentOS system failures. Here are some common steps and techniques: 1. Check the log file /var/log/messages: system log, which contains various system events. /var/log/secure: Security-related logs, such as SSH login attempts. /var/log/httpd/error_log: If you use the Apache server, there will be an error message here. 2. Use the diagnostic tool dmesg: display the contents of the kernel ring buffer, which helps understand hardware and driver questions

Deploying a ZooKeeper cluster on a CentOS system requires the following steps: The environment is ready to install the Java runtime environment: Use the following command to install the Java 8 development kit: sudoyumininstalljava-1.8.0-openjdk-devel Download ZooKeeper: Download the version for CentOS (such as ZooKeeper3.8.x) from the official ApacheZooKeeper website. Use the wget command to download and replace zookeeper-3.8.x with the actual version number: wgethttps://downloads.apache.or

Deploying Hadoop Distributed File System (HDFS) on a CentOS system requires several steps, and the following guide briefly describes the configuration process in stand-alone mode. Full cluster deployment is more complex. 1. Java environment configuration First, make sure that the system has Java installed. Install OpenJDK with the following command: yumininstall-yjava-1.8.0-openjdk-devel Configure Java environment variables: echo "exportJAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk">>/etc/profileecho"ex

Laravel's core functions in back-end development include routing system, EloquentORM, migration function, cache system and queue system. 1. The routing system simplifies URL mapping and improves code organization and maintenance. 2.EloquentORM provides object-oriented data operations to improve development efficiency. 3. The migration function manages the database structure through version control to ensure consistency. 4. The cache system reduces database queries and improves response speed. 5. The queue system effectively processes large-scale data, avoid blocking user requests, and improve overall performance.

ZooKeeper troubleshooting guide for CentOS Systems This article provides a step-by-step guide to help you effectively troubleshoot ZooKeeper faults on CentOS systems. 1. Verify the status of ZooKeeper service: First, use the following command to check the status of ZooKeeper service: sudosystemctlstatuszookeeper If the service is not running, use the following command to start: sudosystemctlstartzookeeper To enable it to start by starting: sudosystemctlenablezookeeper2. Analyze the ZooKeeper log to check Z

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

Detailed explanation of CentOS server PHP security policy: Building a solid protection system This article will explore in-depth how to build a secure PHP operating environment on the CentOS system, covering multiple aspects such as system level, PHP configuration, permission management, HTTPS encryption and security monitoring, etc., to help you effectively reduce the risk of server attacks. Server security is a continuous improvement process that requires regular review and updates to security policies. 1. System security cornerstone system update: Keep the latest version of the CentOS system and all software packages, install security patches in a timely manner, and plug known vulnerabilities. Firewall protection: Use Firewalld to finely control server network access, and only necessary ports (such as HTTP port 80 and H
