Google App Engine(GAE) Support package for Laravel 5.1.
Currently supported features:
- Generation of general configuration files,
- Mail service provider,
- Queue service provider,
- Database connection,
- Filesystem.
For Lumen see https://github.com/shpasser/GaeSupportLumen.
Pull in the package via Composer.
"require": {
"shpasser/gae-support-l5": "~1.0"
}
Then include the service provider within config/app.php
.
'providers' => [
Shpasser\GaeSupportL5\GaeSupportServiceProvider::class
];
Generate the GAE related files / entries.
php artisan gae:setup --config --cache-config --bucket="your-bucket-id" --db-socket="cloud-sql-instance-socket-connection-string" --db-name="cloud-sql-database-name" --db-host="cloud-sql-instance-ipv4-address" your-app-id
The default GCS bucket is configured unless a custom bucket id is defined using
the --bucket
option.
When --db-name
option is defined at least one of --db-socket
or --db-host
should be defined also.
The mail driver configuration can be found in config/mail.php
and .env.production
,
these configuration files are modified / generated by the artisan command. There is
no need in any kind of custom configuration. All the outgoing mail messages are sent
with sender address of an administrator of the application, i.e. [email protected]
.
The sender
, to
, cc
, bcc
, replyTo
, subject
, body
and attachment
parts of email message are supported.
The modified queue configuration file config/queue.php
should contain:
return array(
...
/*
|--------------------------------------------------------------------------
| GAE Queue Connection
|--------------------------------------------------------------------------
|
*/
'connections' => array(
'gae' => array(
'driver' => 'gae',
'queue' => 'default',
'url' => '/tasks',
'encrypt' => true,
),
...
),
);
The 'default' queue and encryption are used by default.
In order to use the queue your app/Http/routes.php
file should contain the following route:
Route::post('tasks', array('as' => 'tasks',
function()
{
return Queue::marshal();
}));
This route will be used by the GAE queue to push the jobs. Please notice that the route and the GAE Queue Connection 'url' parameter point to the same URL. For more information on the matter please see http://laravel.com/docs/5.0/queues#push-queues.
Cache, Session and Log components are supported via the use of specific drivers / handlers:
- Cache - using the 'memcached' driver,
- Session - using the 'memcached' driver,
- Log - using 'syslog' handler.
The configuration options for the mentioned drivers / handlers are generated by the artisan command
and can be found in .env.production
configuration file.
Google Cloud SQL is supported via Laravel's MySql driver. The connection configuration is added by
the artisan command to config/database.php
under cloudsql
. The connection parameters can be
configured using --db-socket
, --db-name
and --db-host
options via the artisan command.
The database related environment variables are set in .env.production
and .env.local
files.
The production
environment is configured to use the socket connection while the local
configured
to connect via the IPv4 address of the Google Cloud SQL instance. Use Google Developers Console in
order to obtain the socket connection string and enable the IPv4 address of your database instance.
The migrations are supported while working in local
environment only.
To use either the production
or the local
environment rename the appropriate file to .env
.
In order to support Laravel filesystem on GAE the artisan command modifies config/filesystem.php
to include an additional disk:
'gae' => [
'driver' => 'gae',
'root' => storage_path().'/app',
],
and adds the following line to .env.production
file:
FILESYSTEM = gae
The optimizations allow the application to reduce the use of GCS, which is the only read-write storage available on GAE platform as of now.
In order to optimize view compilation the included cachefs
filesystem can be used to store
compiled views using memcached
service. cachefs
does not provide the application with a
reliable storage solution, information stored using memcached
is managed according to
memcached
rules and may be deleted when memcached
decides to. Since the views can
be compiled again without any information loss it is appropriate to store compiled
views using cachefs
.
cachefs
has the following structure:
/ +-- bootstrap +-- cache +-- framework +-- views
'/framework/views' is used to store the compiled views.
Use the following option to enable the feature in .env.production
and/or .env.local
file:
CACHE_COMPILED_VIEWS = true
'/bootstrap/cache' is used to store the services.json
, config.php
and routes.php
files,
in order to control caching of these files use the following options in .env.production
and/or .env.local
file:
CACHE_SERVICES_FILE = true
CACHE_CONFIG_FILE = true
CACHE_ROUTES_FILE = true
In order to use config.php
first generate it using the --cache-config
option of
php artisan gae:setup
command. routes.php
has to be generated using
php artisan route:cache
command.
Cache related options are:
- supported on GAE and/or in local environment as long as
memcached
service is present, - disabled while executing
php artisan gae:setup
command.
Additionally the initialization of GSC bucket can be skipped to boost the performance.
In order to do so, set the following option in the app.yaml
file:
env_variables:
GAE_SKIP_GCS_INIT: true
the storage path will be set to /storage
directory of the GCS bucket and storage
directory structure creation will be skipped.
If not used the filesystem initialization can be removed to minimize GCS usage. In order to
do so, remove the following line from .env.production
file:
FILESYSTEM = gae
To support artisan
commands while running on GAE the package provides Artisan Console for GAE
.
The console is implemented as a separate service and not enabled by default for security reasons. To use the console securely /artisan
route has to be protected.
Include the service provider within config/app.php
.
'providers' => [
Shpasser\GaeSupportL5\GaeArtisanConsoleServiceProvider::class
];
Add /artisan
URL handler to app.yaml
file.
handlers:
- url: /artisan
script: public/index.php
login: admin
secure: always
- url: /.*
script: public/index.php
/artisan
URL handler has to appear before the last one (url: /.*
), otherwise it will be ignored by GAE.
The suggested handler secures the route using GAE URL security options. For more information see https://cloud.google.com/appengine/docs/php/config/appconfig#PHP_app_yaml_Secure_URLs.
Enter URL http://your-app-id.appspot.com/artisan in your browser and use the displayed form to submit artisan
commands.
Since GAE's filesystem is read-only the commands will not be able to perform write / update operations on it. For the same reason migrations have to be prepared on local development environment before the deployment takes place. Since the console is not really interactive all the commands are executed in non-interactive mode(by automatic appending of -n
option).
Backup the existing .env
file if needed and rename the generated .env.production
to .env
before deploying your app.
Download and install GAE SDK for PHP and deploy your app.
As of now Laravel scheduled commands are not supported while running on GAE.
In order to use Artisan Console for GAE
the application class app/Console/Kernel
has be edited and any of the commands scheduled using its schedule()
function should be removed.