The Situation
Currently, there are two separate cakePHP projects(apps) where implementation is done and running smoothly of course with different database.
Now, the requirement is to merge these two separate cakePHP apps into one with same database and to run on different domains.
The Problem
1)There exists two different login functions which should be one after merging.Like this there may be some other huge code should be written common.
So, please provide solution or direct me to right way how can one manage common code for multiple apps with different domain
e.g., https://www.sample-1.com/login
and https://www.sample-2.com/login
which is using same database to validate users.
2) How to distinguish routes?
3) How to manage such projects which will grow later?
For both domian you may call common database file and common app folder.
Then you can switch database depends upon request action
Try this
if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
$config = ConnectionManager::getDataSource('defaultCompany')->config;
}
}
inside database.php file
var $defaultCompany = array(
'driver' => 'mysql',
'persistent' => false,
'encoding' => 'utf8',
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => '',
'prefix' => '',
'port' => '',
);
Or if you want common file in both domain. Create plugins outside app folder directory and put inside it.
Related
I have two web Applications. I will login in to one Web Application and will navigate to another by links or redirection from the first Application. Lastly after completing some steps in Application two, I will be redirected to Application one. How can I implement this?
Create a new database called sessions.
Configure a connection profile for the session database secondary to your apps primary databases for both apps.
And then they should be syncing up in storing the data for sessions, being able to share them etc...
config/database.php
'app_main_database' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'sessions_database' => [
'driver' => env('DB_CONNECTION_SESSION'),
'host' => env('DB_HOST_SESSION'),
'port' => env('DB_PORT_SESSION'),
'database' => env('DB_DATABASE_SESSION'),
'username' => env('DB_USERNAME_SESSION'),
'password' => env('DB_PASSWORD_SESSION'),
],
Configure session.connection to the name for your session driver
config/session.php
<?php
use Illuminate\Support\Str;
return [
'driver' => env('SESSION_DRIVER', 'database'),
'connection' => env('SESSION_CONNECTION', 'sessions_database'),
Your question starts with "Laravel - How do..." which leads me to believe both of your applications are using Laravel. You said they are both on the same server and same domain so you could simply expand the default PHP Session cookie from the SLD (Second Level Domain) scope of yourdomain.com to be a cross subdomain cookie: *.yourdomain.com and then set the same application key and your applications will magically start using the and sharing the data. Just make sure that you did the right work in sharing the user database because Laravel default Auth will take the User ID column and log the user into the same ID.
Make sure the environment variables in the .env for both installations have the same identical settings:
#APP_KEY is used to encrypt the session data so must be the same
APP_KEY=base64:'YOUR KEY '
#SESSION_COOKIE must be the same, this is normally not set and defaults to APP_NAME+"_session' which is a problem if your apps have different names
SESSION_COOKIE=laravel_session
#The SESSION_DOMAIN defaults to null which causes the CORS scope to limit to the subdomain level, in my testing must start with leading dot.
SESSION_DOMAIN=.rootdomain.com
Not sure if this is the right way to do things, but was wondering if anyone had some ideas or advice on how to achieve this.
I want to create a subdomain called demo.mysite.com
and i want this to load my codeigniter application which is in public_html, however if the sub domain is demo. then it loads another database instead of the main one
The following is from config/database.php
$db['default']['username'] = "main";
$db['default']['password'] = "password!";
$db['default']['database'] = "mainsite";
The reason I want to do this is i still make changes to my site and dont want to have to keep copying my entire site into a demo folder everytime i make a change.
Hope this makes sense
Create two subdirectories in config dir
aplication
config
site1
site2
Move diiferent files of config dir to that subdirs
In index.php set ENVIRONMENT constant to site1 for one site and site2 for another. To set correct error reporting, add site1 and site2 into switch operator below with a desired state of reporting
define('ENVIRONMENT', 'site1');
switch (ENVIRONMENT)
{
case 'development':
case 'site1':
...
CI get config files from subdir named as ENVIRONMENT
In config database.php you may create different db groups, the one that already exists is the default group:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
...
You can create another group with any name:
$db['another_group'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
...
You can then connect to another database by using:
$this->load->database('another_group', TRUE);
You will find more details in the docs here
I have strange issue. I have two laravel projects on the same server(local server) and I want to retrieve data from one project into another. Each project has its own database and credentials. Now about problem: When I am sending GET/POST request using curl(), another project(destination project) uses credentials of the project I am sending request from. So select statement does not work due credentials and table names mismatch. When I dumped uri and pasted it into web browser it works as expected. Now question: Why second project using database credentials(or maybe other configs too) of the project I am sending request from? What is this shared session or something like this?
I am having a little trouble getting your question, but I am assuming you are using credentials from one project and are somehow getting a return in your other project which has different credentials. On top of that, you also have different databases. How could that be? Well to put it short, it shouldn't be. Can it be done? Yes, but you would have to do it on purpose. There is no shared session. So I would check your database connections in both projects to see if there is some overlap. If there is, check those out as your issue may be in there. If that is not the case, then check your curl request and see if you are passing the correct project credentials and are therefore getting the right result. Other than that, I would need more information to help you out.
Thanks!
And oh, are you using Laravel Passport? If not, what are you using/doing?
Did you solve this problem ?
I faced the same situation and found solution about this.
Solution is to change the .env file of one project as follows.
change .env as follows.
DB_X_HOST="localhost"
DB_X_DATABASE="other_project"
DB_X_USERNAME="homestead"
DB_X_PASSWORD="secret"
DB_X_PORT="3306"
change config\database.php as follows.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_X_HOST', '127.0.0.1'),
'port' => env('DB_X_PORT', '3306'),
'database' => env('DB_X_DATABASE', 'forge'),
'username' => env('DB_X_USERNAME', 'forge'),
'password' => env('DB_X_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
]
It worked fine for me.
Using Codeigniter 3.0.6
Until now I have been using the standard setup for Codeigniter. I have a database and all connects just fine. The question that I'm having trouble with is that I want to add a 'global_accounts' database, from which, depending on the user currently logged in will choose the database to load.
Global_Accounts
--------------------------------------------------------
user | coolUser | user2
pass | coolPassword | passy
url | coolestsite.mysite.com | u2.mysite.com
database_name | coolestsite_application_db | u2_application_db
The application data/schema doesn't really matter as far as I'm concerned. What I want (and am going to set up, but outside of the scope for this question) is for when a user registers, they will fill out user, pass and url, and db_name will be automatically created. The script will then create a new database with that name, creating a blank copy of the application db for the user.
So for now, we're hard coding that, so we can assume that it's working perfectly.
What I want is inside Codeigniter's database.php file:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'coolsite_application_db'
...etc
);
Instead of hard coding this file, I want to be able to do something like this:
$db['global'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'global_accounts'
...
);
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => $this->db['global']->get_db_name($loggedInUserId);
...etc
);
Hopefully that makes sense - I need to get the db name based on the logged in user. Obviously I need to build either a Model or just a function to grab it, but the question is where? Do I add a model, connect to the global db, and then load the model in the database config? Is there some other, easier, more productive way to do this, or is that it? Will the codeigniter core files be able to be called from inside the codeigniter config files?
Use 2 database connections. 1 for the global user accounts and 1 for the actual user database. Then create a helper function to grab the user databases. Just an idea.
I have been using cakephp for creating web application. In my current project there are two database users one for admin another for site users, how can I configure cakephp so that the admin can login to the site with more database operations power ?
Thank you
I agree. Sometimes, it's better having permissions handled in your application layer than the database layer. However, if you really, really want to have that extra layer of security in your database as well, then you should set up multiple database connections:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'normaluser',
'password' => '',
'database' => 'db',
'prefix' => '',
);
var $admin = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'adminuser',
'password' => '',
'database' => 'db',
'prefix' => '',
);
You can then use $this->ModelName->setDataSource('admin') if the user is in the admin section, or whatever condition that you might impose.
I would suggest that you look at the admin_ prefix routing. CakePHP lets you handle admin powers quite easily. Prefix Routing Additionally, you can add a field in your users table to indicate the role of the user, and check that against the current prefix.
the most robust solution will likely be setting up Access Control Lists(ACLs). This will allow you to delegate permissions based on a user role that you designate.
For example admin has a group_id of 1, and users have a group_id of 2. Then you can allow admins to have access to certain operations within your web app.
Here's the cake documentation on this feature.
http://book.cakephp.org/view/1242/Access-Control-Lists