Laravel 5.1 on SAE Environment Development tutorial [project demo source code], laravelsae

Source: Internet
Author: User
Tags bootstrap classes

Laravel 5.1 on SAE Environment Development tutorial [project demo source code], laravelsae

This article describes how to develop Laravel 5.1 on SAE. We will share this with you for your reference. The details are as follows:

Laravel-simple and elegant PHP development framework, a PHP framework created for WEB artists, is now officially transplanted to the SAE environment.

Because Laravel 5.1 has many changes compared with Laravel 4, not only the directory structure is clearer, but also the functions are richer. However, Laravel does not officially support the SAE environment (it is estimated that it will never be supported), so I made a porting version to switch the local environment and the SAE environment elegantly.

Due to the special nature of SAE, these core issues must be solved.

#1 putenv () function disabled
#2 template compilation
#3 cache class
#4 log processing
#5 Session
#6 service provider Cache

#1 putenv () function disabled

Laravel 5.1 uses the putenv () function to dynamically add variables to the current environment. However, it is a pity that the PHPRuntime function of SAE has been disabled, therefore, you can only use the discount method. I originally wanted to Hook up the implementation, but later I thought It was unnecessary. This function is mainly used in Laravel 5.1 to use the. env configuration file for unified team configuration. Therefore, I disabled This function directly. about 86 lines of php, comment out the function directly, and then write all the configuration information to the corresponding configuration file in the config folder. Although the function is disabled, the implementation is not elegant enough.

#2 template compilation

This problem is mainly because writing to the local environment of SAE is forbidden, so I used Wrapper to write the compiled template file to Storage. It was originally intended to be written to KVDB, but some strange problems may occur, for unknown reasons.

In the config \ view. php file, modify:

$compiled = [ 'paths' => [ realpath(base_path('resources/views')), ], 'compiled' => realpath(storage_path('framework/views')),];if(SAE){ $compiled['compiled'] = 'saestor://'.SAE_STORAGE.'/compiled';}return $compiled;

Note that you must create a compiled folder in the corresponding Storage.

#3 cache class

Laravel 5.1 does not directly provide the available Memcache cache driver for SAE. This solution is relatively simple. You can directly write a service provider to register with the app. php, and then in config \ cache. php registration, the specific implementation depends on the project source code

#4 log processing

This is also a tricky issue, because Laravel 5.1's log processing is no longer using the service provider like 4 and is directly injected into the starter, this allows us to override the native ConfigureLogging startup class, and the official team does not show how to overwrite and where to overwrite it, therefore, my solution here is to determine that the current environment is SAE and then directly rewrite an initiator attribute in the Http kernel. The core code is:

namespace Illuminate\Cloud\SAE;use App\Http\Kernel as DefaultKernel;class Kernel extends DefaultKernel{ /** * The bootstrap classes for the application. * * @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', ];}

This is not the case. You must re-write 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

The session of Laravel5.1 is still a local write problem. For details, refer to the transplantation of Laravel4 and use memcache as the session implementation. The specific part can be handled in conjunction with the cache part.

#6 service provider Cache

During the application startup process, laravel will be in the bootstrap/cache/services. the cache of the service provider is generated in json format. To speed up the next access, it is still a local write problem. The solution is very simple. You can use the Storage Wrapper.
After these problems are solved, they are almost successful. Finally, modify bootstrap \ app. php to implement an elegant switch between the local environment and the SAE environment, mainly to determine the environment, generate an SAE private application instance, and inject the corresponding Http kernel.

/*|--------------------------------------------------------------------------| Create The Application|--------------------------------------------------------------------------|| The first thing we will do is create a new Laravel application instance| which serves as the "glue" for all the 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 into the container so| we will be 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;

Here we will explain why we need to use bootstrap \ app. php defines whether it is an SAE environment. The reason is clear: to inject corresponding application instances and Http instances, and then define Storage

Then, configure config \ app. php and inject 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, let's talk about the difference between the SAE proprietary application instance and the Http instance and the native instance, mainly due to local writing. Native will generate relevant files for routing, configuration, service provider, and template compilation when the application is started to increase the loading speed. However, when SAE is reached, the path-related methods of the Application class are rewritten to generate these files into Storage, while the Http proprietary kernel is the log class in the processing initiator. The specific code will not be posted. You can look at the project.

Give another rewrite that SAE can use.

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

Summary

The entire porting process was quite smooth, thanks to Laravel's scalability and the convenience of SAE. however, in terms of the putenv () function and log processing solutions, the implementation is not elegant enough. I hope someone can provide more elegant implementation solutions. Other SAE services, such as Word Segmentation, mail, and queue, can be automatically loaded by the service provider.

Github address: https://github.com/wh469012917/laravel5-on-sae

Click here for SoftwareDownload from this site.

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.