This package allows you to use all the familiar and powerful functionality of laravel as a library for an existing project.
-
Laravel Integration: Use the power of Laravel in any PHP project.
-
HTTP Request handling: With
LaraBridge
you can handle HTTP requests the way Laravel does it, just include the initialization file. -
Including your dependency files: The package primarily aims to ensure that your function files are always loaded and accessible from anywhere: anywhere in Laravel and, of course, in your project code.
-
Does not interfere with standard Laravel behavior: use
public/index.php
andartisan
as usual -
Simple disable error handling: Laravel error handler is very strong. If you are not sure that your project does not contain any warnings or errors, you can manage it.
-
Automatic installation: The package includes scripts for automatic registration
LaraBridgeServiceProvider
and Laravel loading modifications. -
Automatic removal: You can run a script that will completely remove the package and will return all modified files to their original state.
-
Verifying and Rolling Back Changes: Installation and uninstallation scripts check whether the goes through each stage of installation and rolls back changes so that your project does not stop working.
- Laravel: 5.4
- PHP: 5.6
Suppose your project is located in the /path1/path2/my-project
directory. Follow these steps:
-
Install Laravel next to your project, for example in the
/path1/path2/laravel
directory. The name of the folder containing Laravel does not matter. -
Install and configure the
laravel-legacy-bridge
package using Composer in/path1/path2/laravel
. -
Create a file
/path1/path2/my-project/bridgeExample.php
with the following content:<?php include_once __DIR__ . '/../laravel/bootstrap/init.php'; dump('Hello world from Laravel!');
-
You can include
bootstrap/init.php
in any file.
To make this more elegant and maintainable, we recommend to define a LARAVEL_INIT
constant equal to path/to/bootstrap/init.php
and add it to the global PHP constants.
Then your bridgeExample.php
will be like:
<?php
include_once LARAVEL_INIT;
dump('Hello world from Laravel!');
Here we suppose you have a laravel installation. If laravel is not installed, start from the topic before.
-
Install the package via composer
composer require sitroz/laravel-legacy-bridge
-
Then run the register-script.php to register the LaraBridgeServiceProvider in the config app.php
php ./vendor/sitroz/laravel-legacy-bridge/register-script.php
or place service provider to your
app.providers
configSitroz\LaraBridge\LaraBridgeServiceProvider::class,
-
Run the artisan command to embed the code into the standard files and push other files
php artisan laraBridge:install
-
Open
config/laraBridge.php
file and set your old boot files or other parameters as you wish -
Include
.../laravel/bootstrap/init.php
file in any php file outside the Laravel folder You can also include your boot file in the LaraBridge configuration and includeinit.php
to your boot file, protection against recursive includes is configured here.
- Run artisan remove command and remove package in auto mode or follow the instructions if there is any issues.
php artisan laraBridge:remove
- Remove
LaraBridgeServiceProvider::class
fromapp.providers
config - Remove package via composer
composer remove sitroz/laravel-legacy-bridge
By default, laravel handles exceptions and render an exception-page. But if you are not sure that your application is 100% stable and work without errors/warnings/etc. , then it is recommended to disable this behavior so that the application does not break by using laravel.
Important: you can disable error handling only for HTTP requests, CLI-scripts behaviour won't be changed.
For example:
include_once __DIR__ . '/laravel/bootstrap/init.php';
$a[0] = 'Hello';
$a[2] = 'World';
echo $a[0].$a[1].$a[2]; // By default, here your script will be stopped by laravel
function smthImportant(){
echo 'doing our important stuff';
}
smthImportant();
So the smthImportant
won't be done.
To solve it you can disable that by setting config parameter laraBridge.handling_exceptions.disabled
to TRUE
.
In this case you will see a warning and smthImportant
be done.
However, if you want to use an ErrorHandler, you can choose the name
and the secret
in
laraBridge.handling_exceptions.enable_param
config. Then you can enable handler for a single request you do.
Param usage: http://example.com/index.php?name=secret
To protect it from unauthorized use, you can control the configuration value from the ServiceProviders.
LaraBridge provides the ability to handle HTTP requests to any files through a laravel router if you set laraBridge.use_router
to True
.
When router is turned on, laravel will try to find matching route. In success case Router will handle request, send a response just after "include .../init.php" and will be finished.
Mismatch with registered routes not cause any errors/exceptions. The script will continue it's work properly as usual.
There is an extra option rewrite_path_info
. When it sets to True
the path from domain to filename will be added to router path.
The exception to this rule is the index.php
file: the /index.php
substring will be ignored.
URL to file with included init.php | Default Router Path | Router path with 'rewrite_path_info' option |
---|---|---|
example.com/index.php | / | / |
example.com/index.php/p1/p2 | /p1/p2 | /p1/p2 |
example.com/test.php | / | /test |
example.com/test.php/p1/p2 | /p1/p2 | /test/p1/p2 |
example.com/dir1/dir2/index.php | / | /dir1/dir2 |
example.com/dir1/dir2/index.php/p1/p2 | /p1/p2 | /dir1/dir2/p1/p2 |
example.com/dir1/dir2/page.php | / | /dir1/dir2/page |
example.com/dir1/dir2/page.php/p1/p2 | /p1/p2 | /dir1/dir2/page/p1/p2 |
- Try to write PHPUnit test with full workflow including installation, tests and removing the package.
- Test with next laravel versions
Your contribution is welcome! Please send pull requests and create issues to improve this package.
This project is licensed under MIT terms. Details can be found in the LICENSE file.