Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Laravel5.1onSAE environment

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to develop Laravel5.1onSAE environment, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Laravel- 's concise and elegant PHP development framework, the PHP framework created for WEB artists, is now officially ported to the SAE environment.

Because Laravel 5.1 has a lot of changes compared with Laravel 4, it not only has a clearer directory structure, but also has more functions. But Laravel officially doesn't have native support for the SAE environment (and probably never will), so I made a portable version that can gracefully switch between local and SAE environments.

Because of the particularity of SAE, several core problems must be solved.

# 1 disable the putenv () function

Laravel 5.1 uses this putenv () function to dynamically add variables to the current environment, but unfortunately SAE's PHPRuntime disables this function, so it can only be achieved by compromise. I originally wanted to Hook the implementation, but later I didn't think it was necessary. The main purpose of this function in Laravel 5.1 is to use the .env configuration file to unify the team's configuration. So I disabled this feature directly, commented out the function directly on line 86 of vendor/vlucas/phpdotenv/src/Dotenv.php, and then wrote all the configuration information to the appropriate configuration file in the config folder. Although the problem that the function is disabled is solved, the implementation is not elegant enough. I hope a great god can give a more elegant implementation.

# 2 template compilation

The main reason for this problem is that writing to the local environment of SAE is disabled, so I used Wrapper to write the compiled template file to Storage. Originally intended to write in KVDB, but there will be some strange problems, the reason is unknown.

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 the compiled folder is created in the appropriate Storage.

# 3 caching class

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

# 4 Log processing

This is also a thorny problem. Since the log processing of Laravel 5.1 no longer uses the same service provider as 4 and is directly injected into the initiator, we can only overwrite the native ConfigureLogging startup class, and the government has not given how or where to overwrite it. So my solution is to directly rewrite an initiator attribute in the Http kernel after judging the current environment as SAE, the core code:

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 enough, and part of the implementation of the log must be rewritten.

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') {debug' (if) {if $SAE > SAE ($debug') } parent::useDailyFiles ($path, $days, $level);}}

# 5 Session Class

The session of Laravel5.1 is still a local writing problem. Referring to the migration of Laravel4, we use memcache as the implementation of session, which can be dealt with in combination with the cache part.

# 6 Service provider caching

During the startup of the application, laravel will generate the cache of the service provider in bootstrap/cache/services.json. In order to speed up the next access, it is still a problem of local writing. The solution is very simple. You can use Storage's Wrapper.

After the above problems have been solved, it is almost a success. Finally, modify bootstrap\ app.php to achieve the elegant switching between the local environment and the SAE environment, mainly to judge the environment and then generate SAE proprietary application examples 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\ App\ App) } / * |-- | 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 explain why we need to define whether it is a SAE environment in bootstrap\ app.php. The reason is very clear, that is, we need to inject the corresponding application instance and Http instance, and then define Storage here.

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

If (SAE) {$removeProviders = [Illuminate\ Cache\ CacheServiceProvider::class, Illuminate\ Session\ SessionServiceProvider::class,]; for ($I = 0; $I < count ($app ['providers']); $if +) {if ($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'] = Segment'\ Illuminate\ Illuminate\ Cloud\ Cloud}

Finally, let's talk about the differences between SAE proprietary application instances and Http instances and native ones, mainly because of local writing. Native files are generated for routing, configuration, service provider, and template compilation at application startup to speed up loading. But not when it comes to SAE, so rewrite the path-dependent methods of the Application class to generate these 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.

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"

The whole transplant process is very smooth, thanks to the expansion of Laravel and the convenience of SAE. However, the solution to the putenv () function and log processing is not elegant enough. I hope someone can give a more elegant implementation. Then other SAE services, such as word segmentation, mail, queues, and so on, can be loaded automatically by the service provider.

So much for the answers to the questions on how to develop the Laravel5.1onSAE environment. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report