Hosting two different applications running on Laravel 4 on shared hosting - php

I'm about to start development of a site using Laravel4 that will include a cms hosted on a sub-domain. What I want to know if is there is a way to have the main Laravel installation shared between the two apps?
I've had various success in testing using the following example: Laravel full URL routing, however I want to keep the functionality from the app folder separate and have something like say, app_main, app_cms that holds the relevant models, views and controllers for each site in there.
There doesn't seem to be much help that I can find in how to set up Laravel for such a requirement, so any help on this would be great.

I have a multihosting solution, which after logging into FTP contains this folders:
domaina.com
domainb.com
domainc.com
If I'd like to share same Laravel code between those websites,
I just create 'Laravel' folder on the same level, so it looks just like this:
domaina.com
domainb.com
domainc.com
laravel
This 'laravel' folder holds everything except 'app' & 'public' directory.
I just place content of 'public' directory right inside 'domaina.com' folder (for example).
Open up 'index.php' and adjust these lines to match actual location.
Now you are good to go
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';

bootstrap/paths.php holds the main folder definitions. For each instance, you can alter the app folder, public folder, base folder, storage folder and so on from the file.
You can set a cookie/session, or check domain etc. for main and subdomain and alter it like:
'app' => Session::has('subdomain')?'../../app':__DIR__.'/../app',

use Router for this.
e.g.
Route::group(array('domain'=>'example.com'), function(){
//Define the routes for example.com
});
Route::group(array('domain'=>'cms.example.com'), function(){
//Define the routes for cms.example.com
});
for easier maintanace, you can use namespace for controllers.
e.g. your controller folder can look like this
App
|
|---Controllers
|-- site
|
|-----HomeController.php
|-- subdomain
|
|-----HomeController.php
now, for site controllers, use a namspace like, <?php namespace site;?>
for subdomain controllers, use a namespace like <?php namespace subdomain;?>
in the routes file, define the routes as,
Route::group(array('domain'=>'example.com'), function(){
Route::get('/', array(
'as' => 'index',
'uses' => '\site\HomeController#index'
))
});
for subdomains,
Route::group(array('domain'=>'cms.example.com'), function(){
Route::get('/', array(
'as' => 'cms.index',
'uses' => '\subdomain\HomeController#index'
))
});
plain and simplest way.

Related

Splitting Laravel / Lumen routes in multiple files

Lumen framework comes with the routes/web.php file. Reading about how to split the routes in multiple files I came across Laravel documentation (not Lumen) and there it seems pretty clear.
#see https://laravel.com/docs/6.x/routing#basic-routing >>> The Default Route Files
It states
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. ...
Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class
So you can just add other route files and edit the app/Providers/RouteServiceProvider.php class, this seems pretty straight and clear.
Just that Lumen doesn't have any app/Providers/RouteServiceProvider.php class
So then what's the best way to define your own route files without mangling the framework?
Thanks!
We can do this just like Laravel.
Make routes directory in root folder.
Inside routes directory create files, for instance,
like routes/users.php, routes/posts.php
Add above route files, in bootstrap/app.php file
// Load The Application Routes
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
require __DIR__.'/../routes/users.php'; // mention file names
require __DIR__.'/../routes/posts.php';
});
The equivalent in Lumen is located in /bootstrap/app.php.
You can add routes file entries there appropriately. As you can see, there isn't really a specific API for adding files or anything. So just write the logic as you see fit.
If your routes are located in another folder like app/Api/V1 and your controller are located in app/Api/V1/Controllers then you can use the below code in bootstrap/app.php
Folder Structure
For route : app->Api->V1->routes.php
For Controller : app->Api->V1->Controllers
Code:
$app->router->group([
'namespace' => 'App\Api\V1\Controllers',
], function ($router) {
require __DIR__.'/../app/Api/V1/routes.php';
});

Symfony3 routing not working with web root configuration

I am trying to integrate some new Symfony3 apps into an existing web space. At my webroot /html, each app has it's own directory. Each of these apps could be anything- cakePHP, custom PHP, whatever. And each are accessed by a URL like localhost/appname. This structure is not flexible and I am not able to add anymore URL patterns to vhosts or anything like that.
Therefore, I have my Symfony3 install at /symfony which is a sibling directory of /html. Inside of /html I have a landing directory for my Symfony app: /html/symfonyapp.
In /html/symfonyapp/index.php I have one sole line of code:
require_once DIR.'/../../symfony/web/app.php';
In my Symfony set up, I have a bundle called SymfonyappBundle. I have a route configured in src/SymfonyappBundle/Resources/config/routing.yml to redirect calls to /symfonyapp to this particular Bundle.
The routing is not working. Calls to http://localhost/symfonyapp always end up going to the routing for "/" Why? I feel that it has nothing to do with my Symfony setup, but instead something to do with the request coming in through that /html/Symfonyapp/index.php file.
Any help is greatly appreciated!
Edit: I see it's helpful to list out the directory structure so here it is:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|-symfonyapp
|—index.php (which contains only a require for app.php)
-/var/www/symfony <—symphony standard install here
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
I hope I got this right. Your directory structure looks something like this:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|- symfony <-- the project root for you symfony-app
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
So basically you have a typical Symfony-application, but it sits inside a shared web root. So when accessing it you don't go to http://example.com/, but instead to http://example.com/symfony/web/
This might be the first problem you are having. Your application must be accessed from the web folder, not from the symfony-folder. Depending on for example some rewrite rules in /var/www/html/.htaccess you might not be able to look through files in the symfony-subfolder and there is no entry script, so it will not work. Dependening on your setup you might not even have permission to rewrite the url per .htaccess or in your server's config, this would complicate things a bit further. For now let's assume the .htaccess-file in web/ does work and it's just a matter of the wrong folder your url is pointing at.
There are multiple options you have if you want the url to be accessible at http://example.com/symfony/ (without the web/-part). Symfony's project structure is actually pretty flexible and you could get rid of the symfony/web/-folder and instead use symfony/ as your web root. There might be some gotchas for example with some install scripts that copy resources like css and js into your web-root. You could also run into issues when bundles point to the web-directory, e.g. for storing uploads. You probably have to make a few adjustments but a basic setup should be doable in no time by moving all files from web to the parent folder (including the .htaccess which might be hidden).
Another option might be to create a new .htaccess in symfony's project root that points to web/app.php instead of just app.php. You could take the existing file as a reference. I try to avoid using htaccess-files and don't have a setup right now were I could try it, but it might be worth a shot before moving lots of files around. Although you still might run into issues with assets where the path is not matched correctly.
edit: another option that's probably more work, but might be useful if you want to migrate away from the other existing web apps to just a Symfony app is, to move symfony to the same level as html/ and move all the stuff from web/ into html/. Now your server's web root is also symfony's web root (again you might have to fiddle around with assets expecting to be in a folder called web/). Now you just need to make sure that whenever Symfony does not find a route it will pass the request to your other apps. There are several things to look out for and it's a lot more work than the 2 approaches above, but in can be useful. There was a pretty good talk about it at last year's SymfonyCon in Berlin on how to do this if you are interested in this route:
https://github.com/SymfonyCon/2016-talks#modernizing-with-symfony
https://slidr.io/derrabus/modernizing-with-symfony
Unfortunately the video of the talk is not out yet.
The solution that I found was to add a custom Kernel for each web application. So for each folder I have under my webroot /var/www/html, I added a new Kernel in Symfony. Each Kernel has it's own routing and config files, which solved my routing issues! I followed this symfony doc to set up the kernels: http://symfony.com/doc/current/configuration/multiple_kernels.html
App1:
webroot: /var/www/html/app1/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App1Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app1. I copied all config, routing, service files into this directory and reference the custom Bundle for the app.
App2:
webroot: /var/www/html/app2/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App2Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app2. I copied all config, routing, service files into this directory and reference the custom Bundle for the app and changed any references to Bundles to the appropriate bundle.

Laravel project without frontend

currently I have a standard Laravel 5 proyect, eg:
Laravel Directory
app Directory
bootstrap Directory
config Directory
database Directory
public Directory
resources Directory
routes Directory
storage Directory
tests Directory
vendor Directory
what I'm trying to do is to take the public and resources folder out from the application and put it them on a different project, and in that way I'll use the laravel part only for backend purposes and the frontend parte I'll manage it on an outside project. e.g.:
Laravel Directory
app Directory
bootstrap Directory
config Directory
database Directory
routes Directory
storage Directory
tests Directory
vendor Directory
Frontend project
index.html
app folder
css folder
assets
Any recomendation or ideas to do it ?
Removing (or moving) the Public folder is not a good idea, especially due to the fact that the public/index.php file is the entrypoint for the application.
I personally use both laravel and lumen for a bunch of my REST-API's and it works great, so that thought is not at all wrong.
Just ignore the views, don't use them and don't expose them from any controller action, but rather return all data from the controllers as JSON instead.
This is easily done from the controller actions like:
public function getSomethingAction() {
return response()->json([
"some" => "property"
]);
}
// Which will produce the following json (including headers and all):
{
"some": "property"
}
I would also recommend that you group your routes under a API namespace of some sort:
Route::group(["prefix" => "api/v1"], function() {
Route::get('something'...
});
So now when you call domain.tdl/api/v1/something you will get a neat json response!

how to define laravel 5.0 routes by means of directories

I would like to do this thing with laravel 5.0.
I would have many routes.php files in some folders (like, under the app/ folder, I create a "linux" folder and a "windows" folder). Inside the two linux and windows folders, i put two routes.php files, with different routes (like, i don't know, they both have the route get('os') ).
What I want is that calling public/windows/os it is called the route os of the windows folder, and calling public/linux/os it is called the route os of the linux folder (basic polimorphism).
I searched over and over and found nothing helpful. Do you have some advice?
Thanks.
You can simply include the other route files in your main routes.php and put them inside route groups:
Route::group(['prefix' => 'windows'], function(){
include app_path('windows/routes.php');
});
Route::group(['prefix' => 'linux'], function(){
include app_path('linux/routes.php');
});
To reduce the duplicate code you can also use an array to configure your subdirectories:
$systems = ['windows', 'linux'];
foreach($systems as $os){
Route::group(['prefix' => $os], function(){
include app_path($os.'/routes.php');
});
}

Yii Share models between two applications

I've two web applications which are located on one server but on different domains (for example app A is administration and app B is client).
The problem is that I want to share models (ActiveRecords) from application A to be available in application B.
Is there any clever way to do this?
Thanks!
Try to make an alias in one app to the second ( And from the 2nd to the 1st ;) ) using
YiiBase::setPathOfAlias()
Documentation:
http://www.yiiframework.com/doc/api/1.1/YiiBase#setPathOfAlias-detail
Sure, just follow a few easy steps:
1. Put your models in a shared directory
For example, if your current directory structure looks like this:
/www
/application1
/protected
/models
/application2
/protected
/models
Create another "shared" directory. It's a good idea to put some structure in there as well, in case you want to share more than just some models:
/www
/application1
/protected
/models
/application2
/protected
/models
/shared
/models
Put the active record models you want to share in /www/shared/models.
2. Alias the shared directory in both applications
Go to your main.php configuration file in both applications and create an alias for the shared directory:
Yii::setPathOfAlias('shared','../shared/'); // or use an absolute path
3. Import shared models
Still in your main.php configuration, import your shared models:
'import'=>array(
// ...existing imports here...
'shared.models.*',
),
You can now directly refer to the shared classes anywhere in your application and Yii will load the appropriate classes automatically.
If you later add more directories to /shared then simply add corresponding lines to the import configuration.
Yii::setPathOfAlias('applicationA','path/to/applicationA/protected');
Then when you making import in config:
'import' => array('applicationA.models.*'....
Now you will be able to use models from appA in appB.
Same can be done with modules, controllers and views.
Views - viewPath
Modules - modulePath
Controllers - in index.php add
$app->setControllerPath('////protected/controllers');
Before $app->run();

Categories