Laravel 5.1 On SAE Environmental Development Course "with Project Demo source" _php Example

Source: Internet
Author: User
Tags aliases goto php framework php programming wrapper smarty template

This article describes the Laravel 5.1 on SAE environment development method. Share to everyone for your reference, specific as follows:

laravel-Concise, elegant PHP development framework for WEB artists to create a PHP framework, now officially ported to the SAE environment.

Since Laravel 5.1 has many changes compared to Laravel 4, it is not only more clear in the directory structure, but also richer in functionality. But Laravel officially does not have the original support SAE environment (estimated will never support), so I made a transplant version, can be very elegant switch local and SAE environment.

Because of the particularity of SAE, these core problems have to be solved

#1 putenv () function disabled
#2 Template Compilation
#3 Cache Class
#4 log Processing
#5 Session Class
#6 Service Provider Cache

#1 putenv () function disabled

Laravel 5.1 uses this putenv () function to dynamically add variables to the current environment, but unfortunately the SAE Phpruntime disables the function, so it can only be achieved using a compromise approach. Originally wanted to hook up the implementation, later felt unnecessary, this function in Laravel 5.1 is mainly to use the. env configuration file to unify the team's configuration. So I was directly disabling the feature, in the vendor/vlucas/phpdotenv/src/dotenv.php of the 86 line, directly commented out the function, and then all the configuration information is written to the Config folder in the appropriate configuration file. Although it solves the problem that the function is disabled, the implementation is not elegant enough, hoping that the great God can give a more elegant implementation.

#2 Template Compilation

This problem is mainly due to the fact that the SAE's local environment write is forbidden, so I used wrapper to write the compiled template file to storage. Originally intended to be written into the KVDB, but there will be some strange problems, unexplained.

Modify in config\view.php file:

$compiled = [
 ' Paths ' => [
 realpath (Base_path (' resources/views ')],
 ],
 ' compiled ' => realpath ( Storage_path (' framework/views ')),
];
if (SAE) {
 $compiled [' compiled '] = ' saestor://'. Sae_storage. ' /compiled ';
}
return $compiled;

Note To establish the compiled folder in the appropriate storage.

#3 Cache Class

Laravel 5.1 does not directly provide the SAE available Memcache cache driver, this solution is relatively simple, directly write a service provider registered to App.php can, and then registered in config\cache.php, the specific realization of the project to see the source code

#4 log Processing

This is also a tricky problem, because the Laravel 5.1 log processing is not and 41 of the use of service providers, and directly injected into the launcher, which allows us to overwrite the original configurelogging start class, and the official did not give how to overwrite and where to overwrite, So my solution here is to determine the current environment for the SAE after directly rewriting an initiator attribute in the HTTP kernel, core code:

namespace Illuminate\cloud\sae;
Use App\http\kernel as Defaultkernel;
Class Kernel extends defaultkernel{/** * The bootstrap for the classes
 .
 *
 * @var array
 /
 protected $bootstrappers = [
 ' illuminate\foundation\bootstrap\detectenvironment ',
 ' illuminate\foundation\bootstrap\loadconfiguration ',
 ' illuminate\cloud\sae\log\configurelogging ',
 ' illuminate\foundation\bootstrap\handleexceptions ',
 ' illuminate\foundation\bootstrap\ Registerfacades ',
 ' illuminate\foundation\bootstrap\registerproviders ', '
 illuminate\foundation\ Bootstrap\bootproviders ',
 ];
}

Not yet, you must also rewrite the partial implementation of the log

Class Writer extends Illuminatelogwriter {
 protected function usesaelog ($level = ' Debug ') {
 $level = $this-> Parselevel ($level);
 $this->monolog->pushhandler ($handler = new Saeloghandler ($level));
 $handler->setformatter ($this->getdefaultformatter ());
 }
 Public Function Usefiles ($path, $level = ' Debug ') {
 if (SAE) {return
 $this->usesaelog ($level);
 }
 Parent::usefiles ($path, $level);
 }
 Public Function Usedailyfiles ($path, $days = 0, $level = ' Debug ') {
 if (SAE) {return
 $this->usesaelog ($level );
 }
 Parent::usedailyfiles ($path, $days, $level);
 }


#5 Session Class

Laravel5.1 session is still a local write problem, referring to the Laravel4 transplant, using the memcache as the implementation of the session, specific can be combined with the cache part to deal with

#6 Service Provider Cache

During the application startup process, Laravel will be bootstrap/cache/ Services.json generate a service provider's cache, in order to speed up the next visit, is still a local write problem, the solution is very simple, using storage wrapper can be
above these problems resolved, almost even if successful. Finally modified bootstrap\app.php to achieve the local and SAE environment elegant switch, mainly to judge the environment and then generate SAE proprietary application instance and inject the corresponding HTTP kernel.

/*
|--------------------------------------------------------------------------
| Create the application |--------------------------------------------------------------------------| | The ' The ' I thing we do are create a new Laravel application instance | Which serves as the "glue" for all components of Laravel, and is |
The IoC container for the system binding all of the various parts.
|
* * Define (' SAE ', true);
Define (' Sae_storage ', ' laravel '); if (SAE) {$app = new illuminate\cloud\sae\application (Realpath __dir__. ') /..
 /')
 );
$app->singleton (Illuminate\contracts\http\kernel::class, Illuminate\cloud\sae\kernel::class); }else{$app = new Illuminate\foundation\application (Realpath (__dir__. ') /.. 
 /')
 );
$app->singleton (Illuminate\contracts\http\kernel::class, App\http\kernel::class); }
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces |--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces in container so | We are able to resolve them when needed. The Kernels serve the |
Incoming requests to this application from both the Web and CLI.
|
* * $app->singleton (Illuminate\contracts\console\kernel::class, App\console\kernel::class);
$app->singleton (Illuminate\contracts\debug\exceptionhandler::class, App\exceptions\handler::class); /*
|--------------------------------------------------------------------------
| Return the application |--------------------------------------------------------------------------| | This script returns the application instance. The instance is given to | The calling script so we can separate the building of the instances |
From the actual running of the application and sending responses.
|

 * * return $app;

This explains why the SAE environment is defined in bootstrap\app.php, because it's clear that you inject the appropriate application instance and HTTP instance, and then define the storage

Then there is the config\app.php configuration, which injects the corresponding service provider according to the environment judgment.

if (SAE) {
 $removeProviders = [
 illuminate\cache\cacheserviceprovider::class,
 illuminate\session\ Sessionserviceprovider::class,
 ]; 
 for ($i = 0; $i < count ($app [' providers ']) $i + +) {
 if (In_array ($app [' providers '] [$i], $removeProviders)) {
 unset ($app [' providers '] [$i])
 ;
 }
 $app [' providers '] = Array_merge ($app [' providers '],[
 Illuminate\cloud\sae\cache\saecacheserviceprovider:: Class,
 Illuminate\cloud\sae\session\sessionserviceprovider::class,
 illuminate\cloud\sae\storage\ Storageserviceprovider::class,
 Illuminate\cloud\sae\segment\segmentserviceprovider::class,
 ]);
 $app [' aliases '] [' Storage '] = Illuminate\cloud\sae\storage\storage::class;
 $app [' aliases '] [' Segment '] = Illuminate\cloud\sae\segment\segment::class;
}

Finally, the SAE proprietary application instance and HTTP instance differ from native to native, and are mainly local write problems. The native will increase the load speed by generating the routing, configuration, service provider, and template-compiled related files at application startup time. But the SAE does not work, so the partial path-related methods of the application class are rewritten to generate the files into storage, while the HTTP proprietary kernel handles the log classes in the initiator. The specific code will not be posted, you can look at the project.

And give a rewrite that SAE can use.

Handle:
 -rewrite:if (path ~ "^/$") goto "public/index.php"
 -rewrite:if (!is_dir () &&!is_file () &&A mp path~ "^ (. *) $") goto "public/index.php/$1"

Summarize

The entire transplant process is very smooth, thanks to the laravel and SAE convenience. But in the solution to the putenv () function and log processing, the implementation is not elegant enough, I hope someone can give a more elegant implementation. Then other SAE services such as participle, mail, queues, etc., can be automatically loaded using the service provider, this is not much to say.

Project GitHub Address: Https://github.com/wh469012917/laravel5-on-sae

Software click here to download the site .

More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "

I hope this article will help you with your PHP programming based on the Laravel framework.

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.