Finding a good structure for multiple apps in symfony2 - php

This is the situation i am currently having issues with:
I have 1 webserver, this webserver must run 2 different websites, those websites are all written in symfony2 and share the same Bundle that i wrote.
Currently this bundle is placed in the src/ folder and loaded in the AppKernel.
Right now i am using the apps approach where i have 2 seperate app folders created in 1 folder called apps ( http://jolicode.com/blog/multiple-applications-with-symfony2 )
However right now my situation changed, i have this 1 webserver with 2 different websites but i also have a second webserver which must also be able to have multiple apps.
Both servers should not share the same configuration files which is having me a lot of difficulties with deploying.
I also want to prevent that i have to deploy unused bundles to a server.
I hope i explained everything as clear as possible, it is not easy to make a description.

As I read, the app folder contains all config or not files related to the app ( doctrine, Kernel, cache, logs, etc). Routing, Services or other files related to how works an specific bundle should be within that bundle in my opinion.
I think there is not a standard or good way to do this because symfony right know has the workflow one app - one project. Any structure which separates bundle config and app con
fig is valid if don't mix both kinds

Related

Symfony 3 and Angular 2 Folder Structure

I am new to both Symfony and Angular and I am trying figure out how the pieces will fit together. I want to user Symfony for my back end and web api. I want to use Angular for the front end desktop like experiences.
I am not sure how the folder structure should be. Do I put my angular javascript/typescript files in folders in the root of the Symfony project and utilize their folder structure. Or do i have two bundles in my src folder. One for the back end/website. Another for the front end? Or should the back end be serving twig templates with angular components inserted?
***** Edit *****
#Tobias Xy Correct me if I'm wrong but that would be creating bundle in the src folder and keeping the files in a resources folders in it correct?
Then i see the following at the bottom:
Frontend-Based Applications
Recently, frontend technologies like AngularJS have become pretty
popular for developing frontend web applications that talk to an API.
If you are developing an application like this, you should use the
tools that are recommended by the technology, such as Bower and
GruntJS. You should develop your frontend application separately from
your Symfony backend (even separating the repositories if you want).
So they are saying make two seperate projects and not use a bundle for the front end?
If so how do you host this? On two different sites?
Symfony is a backend framework, so there is no point in adding a bundle containing only frontend files (css, javascript, etc).
See also Web Assets (Symfony Best Practices)
They say there:
Store your assets in the web/ directory.
Update 23.03.2016
Cerad wrote in the comments: "client-side angularjs and reactjs apps have their own build system just like Symfony does".
This is actually a different topic, but the article I linked to above also says something about that:
Recently, frontend technologies like AngularJS have become pretty popular for developing frontend web applications that talk to an API.
If you are developing an application like this, you should use the tools that are recommended by the technology, such as Bower and GruntJS. You should develop your frontend application separately from your Symfony backend (even separating the repositories if you want).
For Angular 2 there might be differences in the front end build system, but the structure should be similar to the one I use for Angular 1.x.
The folder structure I use to place my front end assets in something we could describe as a three level process. Let's just enumerate the three levels here:
Level 1 - The src/AppBundle/Resources/Private folder
Inside this folder, I place any front end assets that need any kind of processing, like transpile, autoprefix, uglify, etc. This will be the case for most JS, SCSS, LESS or HTML files of your choice. You can define the folder structure you prefer.
Level 2 - The src/AppBundle/Resources/Public folder
This will be the folder that'll contain all assets after processing AND any other asset that didn't need processing, usually images, fonts, etc. This will be the folder structure that will literally be copied over to the web/bundles/app folder in Level 3.
Note: This level is indeed redundant and can be skipped with caution. In my case, I kept it to avoid 'accidents' with the way Symfony wants to install assets by default, which will replace your web/bundles/app folder with the contents of this one. This will be the case if you use any other bundle that installs assets this way, like FOSJsRoutingBundle for example.
Level 3 - The web/bundles/app folder
This is the final public destination of your assets, and the path where you'll reference them in your code. It is just a copy of the src/AppBundle/Resources/Public folder from level 2.
Build system
You will need a front end build system to transpile your files and copy them to the respective folders in level 2 and 3. In my case for Angular 1.x I used Gulp with Node. This means you'll have gulpfile.js file, package.json file and node_modules folder in your project. I didn't care, it worked fine. Just remember to not add node_modules folder to your repo.
In production
Unless you really need to rebuild in production for some reason, you can skip level 1, level 2 and gulp related folders and files like node_modules, gulpfile.js, etc.
You could create the client side as the root document and the server side as an alias location, but careful to not overwrite the alias.
Or you could change the host.
Example 1:
[client] www.example.com/<client_root_dir>
[server] www.example.com/api/<server_root_dir>
or Example 2:
[client] www.example.com/<client_root_dir>
[server] api.example.com/<server_root_dir>

Laravel Multi-Application Routing

I'm working on a platform which runs multiple Laravel applications that interact via a single database.
Each Laravel instance is responsable for it's own logic, i.e:
Admin module (own subdomain)
User module (own subdomain)
Homepage module (own domain)
Saas-ish module (different domains)
Now the problem im facing with routing is the following:
I want to be able to create/generate URL's for the other modules in my platform and for each of the domains inside the Saas-ish module
Things i've thought about
Option 1
Make routes a shared folder/file (via git submodules or a package) and generate the route file based on the Saas domains and subdomains with the domain group option (and cache it). While this is a viable option, I'd prefer not to load a massive file with all the routes, when all I need is 1 or 2 per request.
Option 2
All routes in the DB. Make a RouteGenerator which will handle this for me. However I'm thorn in this option, because I think it the domains in the Saas module, (which are basicaly entities in my DB) and the other module's subdomains (which arn't entities in my database) are too different. So this may not be the best option.
One of these might be the solution for my problem, and i'm just not seeing it, OR it's something completely else.
Please share your insight...
No. Just no.
Here is your option: Merge.
This is more or less not optional. You never run the same framework multiple times for different parts of an app. It's a waste of space and awful to maintain.
You MUST merge your application into one.

The point of Yii2 environments folder

I am trying to work what the point of the environments folder is.
Originally I had the idea that you could point the webserver to the different dev and prod folders in the environment folder but after reading up a bit I realise this is not the case.
In Yii 1 you would solve this by just having multiple index.php's i.e.:
index.php
index-local.php
So the question is what benefit does this new environment structure actually give me over the old way?
I've found environments very useful in allowing me to keep a common code base for multiple client projects (based on Yii App Advanced) and setting up a different environment for each specific client, keeping their custom code private and separate.
To do this I store the environments folder in a separate git repo from the rest of the code and pull down the relevant folder on a client/project basis.
This lets me use a base common code for all projects and add/override any file for a specific client or project whilst still allowing separate dev/prod config settings. If the client uses other developers too, they are also catered for. In this way, only common code I choose will be shared amongst clients and custom code will be kept private.
I've also moved the composer.json file into the environments folder so I can pull in different extensions per client/project keeping those private too.
That init command can be a very powerful tool and you don't have to limit yourself to the template provided by the core developers.
If you don't need environments, then don't use them, but I assure you some people will find it very useful.
Yii2 documentation in WIP, but you should read this :
https://github.com/yiisoft/yii2/blob/master/docs/guide/apps-advanced.md#configuration-and-environments
You need to use yii init command to switch between these environments.
EDIT :
This new environment feature is more than just use different config file. You can use different folder structure, different entry script...etc
Personnaly I won't use this feature, I don't need it (I will use a different entry script as with Yii 1), but I think this is not useless.
I think you didn't get the real purpose of environments introduced in Yii2.
I'll try to explain what was the main purpose of adding environments to yii from the developers point of view on an example and hope you will really appreciate its usefulness.
Let's suppose for a moment that you are a team of developers (e.g. 5-7 person) working on mid-to-large project implemented in Yii. To effectively work on that project your team decides to use some CVS or SVN (e.g. GIT) and keep all the files of the project in repository in cloud for the whole team. That's de facto standard while working on mid-to-large projects in teams and nobody will resist that it's the only comfortable and easy way.
Ok, now let's suppose you use Yii 1.x or Yii2 with the approach of different entry scripts to differentiate between local (development) and production environments to connect to db or set some other environment specific configs. Everything is ok and working. But suppose your team members implemented something new on the project and you check out repository to work on updated version and you suddenly find out that your local config file (in this case entry script with config) is overwritten with other team member's file who pulled the changes to repository (because each of you is using your local machine db with other database name or OS, or config, or simply because your team uses one local development server db, but you are on vacation and can't use anything except your local machine).
So generally Yii2 environment adds more flexibility for using different environments each with it's own specific configurations while using also general (common) configs when working in teams on mid-to-large projects hence why the example in guide is given on advanced app project.
Surely you can overcome everything stated above with some solutions or .gitignore which is used by default to overcome the problem stated in Yii2 with environments. But:
Why bother if everything is already done?
and
It was just one little example of usefulness of Yii2 environments. More depends on the project and your imagination.
Overall Yii2 is great product. Not only it adds many new features to already great framework, but it also is more robust and flexible than Yii 1.x (despite the fact that Yii 1.x was already very robust).
As for Laravel or any other PHP framework, it really depends... Everyone will find his/her own favorite.
For those who are tired of copying files around, I created a useful script that you can run in background to keep the files in sync on your dev environment:
File sync-env-files.sh
#!/bin/bash
ENVIRONMENT_DIR="/var/www/example.com/environments/dev/"
DIR="/var/www/example.com/"
while [ true ]; do
for envFile in `find $ENVIRONMENT_DIR -type f`
do
file=${envFile/$ENVIRONMENT_DIR/$DIR}
if [ `stat -c "%Y" $file` -gt `stat -c "%Y" $envFile` ]; then
#echo "copying modified file $file to $envFile"
/bin/cp -f $file $envFile
fi
done
sleep 2
done
Then run the script in background or add to cron with flock
nohup server/sync-env-files.sh >/dev/null 2>&1 &
I would like to mention in addition to #AngelCoding, since this question still gets seen, that I use the environments folder lots now and definitely see the point of it.
The very first things I do in any open source project is create one project for the code base on GitHub and then another, private, one on Bitbucket for the configuration, in other words the environments folder.
Having this folder has made it a lot easier for me to separate my configuration into a private repository.
So the environments folder has a lot of uses and really helps to separate configuration for easier usage even if it does not seem like it initially.

Which PHP frameworks have a code base that can be shared accross entire separate sites easily?

I am looking to reduce redundancies in code shared across entire web sites. I have tinkered with several frameworks but cannot think of any that allow you to EASILY separate the framework code from the site code while sharing it to multiple sites at the same time.
What PHP frameworks can do this easily?
EDIT - I am trying to determine which frameworks are the easiest to share.. I was already guessing that nearly all could be shared, but which frameworks are geared towards sharing? It sounds like Yii recommends placing the framework code outside the site code, that is a good start.
If someone is sharing the same framework code across sites already, I would love to know about that.
It's pretty easy to do that with Fuel (http://fuelphp.com).
Each website has an index.php where some paths are defined:
/**
* Set all the paths here
*/
$app_path = '../fuel/app/';
$package_path = '../fuel/packages/';
$core_path = '../fuel/core/';
As you can see, you may share the core and packages in a central repository and create a single app and public folders to each web site.
You may even share an app with different web sites customizing stuff (let's say, the site title or the database used) by just setting a different environment in the .htaccess. That works out-of-the-box for development/stage/production sites, for example, but may be extended to anything. You may also setup central packages to use in multiple apps. Powerful, easy and just works.
Many can do this. For instance YII is supposed to be installed OUTSIDE of your www-root directory (httpdocs, /var/www/ or something like that). You can use several sites to point to that base dir.
Any framework (or part) that does not need specific settings for your site can be shared among multiple sites I guess.
I believe Zend can do what you ask, possibly even Symfony and Fuel, and I'm sure many other frameworks that allow you to pick what parts of it to use will let you do this.
However, doing so will require you to do a little more configuring to get it all running. Which is kind of why I ended up creating my own framework.
Symfony does. I love the Symfony framework, and it comes with some great frameworks. You might like the Routing and YAML ones. A person I know calls Symfony the best php framework.
Symfony components
Some of the components have their own specific sites
You can find a really good documentation here.
Symfony2 is suitable for your needs. It's a full stack framework with a lot of standalone components. It works with "bundles", a bundle is a kind of container with a complete logic (controllers, model objects, views, assets, configuration, ...). That means you write one bundle and you can reuse it without any problem.
But you can also consider symfony 1.4. One project can handles many applications so your model is shared across these applications and the same code can be reused in all applications. Note an application can be a complete website.
I can't think of any frameworks that do this natively, but you could use several SVN (or hg, etc) repositories to accomplish this. Example using CakePHP:
1 repo has the CakePHP default files. If you wish to update CakePHP,
you update this repo in the future.
1 repo per website that stores everything inside your app folder.
It's not built in functionality, but it isn't very difficult to setup either.

Break out shared model in code igniter applications

I'm fairly new to code igniter and to php (coming from a java background).
I want to create a model that's shared by three apps that may or may not run on the same machine.
So I want to keep the model classes portable to be shared by all three and easily handled by SCM systems.
So my thought was to do the following:
If I have already broken the applications out into separate directories to share CI:
/var/www/app1
/var/www/app1/models
/var/www/app1/controllers
...
/var/www/app2
/var/www/app3
/var/www/system
then create a shared folder to put the model in:
/var/www/shared/models
Now I could either hack CI to look for the models there, or I could use symlinks, ie
/var/www/app1/models -> ../shared/models
/var/www/app2/models -> ../shared/models
/var/www/app3/models -> ../shared/models
I'm new to the inner workings of PHP, so I'm thinking at minimum this will create a performance hit because the model files may be loaded up to 3 times (maybe 4), or at worse this'll just barf because there's 4 copies of files named the same thing.
Is this a bad idea? Is there a better way to do this?
Have you considered using Kohana instead? Kohana was initially a fork off CI but it has become an excellent framework on its own. The cascading filesystem feature that you can't find in any other web frameworks is really helpful to have multiple applications to share models/views/controllers/libraries/helpers etc.
Here is how I would do it in Kohana 2.x:
Upload the system and modules folders into a location outside of http root, e.g /apps/kohana/system and /apps/kohana/modules
Put the application folder somewhere nearby, e.g. /apps/kohana/applications/app1
Set up the /public_html/index.php to point to the 3 folders.
$kohana_application = '../apps/kohana/applications/app1';
$kohana_modules = '../apps/kohana/modules';
$kohana_system = '../apps/kohana/system';
Now just throw the model that you want to be shared among your apps into /apps/kohana/system/models/. Or if you want to restrict to only some apps, create a module for example /apps/kohana/modules/mymodule1 and configure those apps to use the module.

Categories