How do I make CakePHP 3 detect production vs live servers? To disable Debugkit - cakephp-3.0

What I do is I set debugkit to false in bootstrap.php
if (Configure::read('debug')) {
Plugin::load('DebugKit', ['bootstrap' => false]);
}
hen I push that to my live server and gitignore bootstrap.php. Then I enable it to true on my localhost
The problem is if I modify bootstrap.php my changes aren't being recorded due to gitignore. I know there must be a better way thanks!

Related

I want to get the backend url from frontend without using hardcoded URLs yii2

'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://backend.test',
'enablePrettyUrl' => true,
'showScriptName' => true,
],
then I want to display the image saved under uploads directory
<img src="<?= Yii::$app->urlManagerBackend->baseUrl; ?>/uploads/logo.jpg>
the problem is this url must not be hardcode like this:
'baseUrl' => 'http://backend.test',
The only way how to dynamically determine the domain of the other application (for example the backend from your frontend) would be by parsing the web server's configuration files.
The domain for current application (the one you can get with Url::base(true)) is determined from the request headers or variables set by web server. But those are available only for current application, not for any other application even if they are part of same project.
If you want to parse web server's configuration files than you will have to face three major challenges:
Different web servers have different syntax for configuration files.
Configuration files might be located anywhere.
You might not have access rights to read the configuration files.
So it might be better to try to think about some workaround instead of insisting on determining the domain dynamically.
Make a deploy script that would ask for the backend domain. The one who will be deploying your application on production servers will know the domain for the backend application and can enter it during deployment process. The deploy script will then set the entered backend domain in your configuration files.
Make a page in backend that must be visited before accessing the frontend application. You can determine the domain for backend when the page in backend is visited then set that domain in frontend configuration files. If the frontend is accessed before the domain for backend is set you will only display the notice that the backend page must be accessed first.
In the config folder there should be a file called params.php. If you have something like this
<?php
return [
'adminEmail' => 'admin#example.com',
'baseUrl' => 'http://backend.test',
];
You can use it in your code like this
<img src="<?= Yii::$app->params['baseUrl']; ?>/uploads/logo.jpg>
Then when you move to live, you just need to edit the params.php file.
Too long comment so I need to put it here.
But I'm just wondering in which case that makes sense, except if you are creating web applications, sites, ..., through your application, which I doubt you do.
You know your local domain (use local environment and put urls).
You will know your dev domain (use dev environment and put urls).
You will know your production domain (use prod environment and put urls).
You can also have multiple applications inside yii2 project, so for example,
10 applications across 3 envs, that is 30 urls which you will enter in you configs.
Can you please tell me, how you will access your app if url is dynamically determined -> without using anything else except Yii?
What is your step? You are typing in your browser what? Then we can proceed. Maybe we misunderstand each other.
urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://backend.test',
'enablePrettyUrl' => true,
'showScriptName' => true,
]
If you are wondering you can also have multiple urlManagerBackend components across Yii2 environments. Just like with params. Add it on multiple corresponding places at config. So in specific environment you place at same file only key => values which you need to override.
You could simply use Assets and Aliases for this:
If you have a backup/web/uploads/ folder in which you save images uploaded via your backend and you'd like to display those images on your frontend.
Create a new asset file in your frontend/assets/, let's call it BackendAsset.php:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
class BackendAsset extends AssetBundle {
public $sourcePath = '#backend/web/uploads';
}
where $sourcePath is the backend-folder (source) you'd like to access on the frontend. The #backend alias is predefined in the advanced template, so we'll use it.
Now in our view we can simply use the BackendAsset:
<?php
use frontend\assets\BackendAsset;
$backend = BackendAsset::register($this);
?>
and now we can easily display a file, let's say backend/web/uploads/somefile.jpg:
<img src="<?= $backend->baseUrl . '/somefile.jpg' ?>" >
NOTE: Using the asset in this way copies all the files from the backend/web/uploads to an asset folder in the frontend. To prevent that, you can tell your application not to copy the files, but to link to them (creating SymLinks) instead, unsing linkAssets (yii2 docu):
In your app configuration (in this case frontend/config/main.php), set the linkAssets parameter to TRUE:
'components' => [
'assetManager' => [
'linkAssets' => true,
]
]
I solve this problem by saving the full url in the database.
What about putting a reverseproxy (e.g. nginx) in front of the frontend-server?
Could be configured like:
http://frontend/backend/* -> forwards everyhing to the backend service, the rest will still go to the frontend server.
The configuration (in this case the location of the backend server) of this reverseproxy can be changed any time (also after deployment).
Could that be a viable scenario?

Cake PHP 3 debug_kit panel

After install CakePHP3 ver. 3.6.2, debug_kit don't show panel. in the log file
"Warning: DebugKit is disabling itself as your host newtest.my is not in the known safe list of top-level-domains (localhost,dev,invalid,test,example,local). If you would like to force DebugKit on use the DebugKit.forceEnable Configure option."
How i can enable debug panel? Thank's!
This question has already been answered by Greg Schmidt but for clarity for anyone else wondering about this in the future: basically what is happening is CakePHP has determined that the host you are using is unsafe, and has therefore disabled the Debug Kit. CakePHP also provides a workaround for this by providing the DebugKit.forceEnable key to override this default behavior. It is recommended that you do this in either app.php or, if you have kept the default app.php as-is and provided an override file like app_local.php as you should, you can do it there as well:
'DebugKit' => [
'forceEnable' => true,
// other config options
]
CakePHP provides a tiny explanation of this in Their Cookbook
An other way to achieve this would be to do something like this in your bootstrap.php
if (Configure::read('debug')) {
Configure::write('DebugKit.forceEnable', TRUE);
Plugin::load('DebugKit', ['bootstrap' => TRUE]);
}

Setting database parameters in Zend 3

I'm a serious newbie as it comes to Zend and I'm now forced to work with Zend 3 (and learn it). Fellow developers have done parts of the application and now I'm cloning it on my side.
I receive "Database not selected" in the error log - I have set database.local.php and I think they've set everything else. Credentials are correct.
I'm working on Windows.
Is there anything else I could be missing as it comes to settings or database connection?
Thanks. I will provide any additional info if needed.
As we're dealing with both Zend Framework AND a configuration issue, for your next question, please make sure to also include file paths and such.
I'm proceeding with this answer under the assumption that you've created your file here: /config/database.local.php.
Note: using ZF3 myself, I, of course, tried to find your error message "Database not selected", however it comes back with no results. Make sure you copy and paste error messages so users of any framework, cms or another system can more easily help you out.
You'll find the problem you're facing in the application.config.php file (in the /config folder. In here you'll find the following config:
// Retrieve list of modules used in this application.
'modules' => require __DIR__ . '/modules.config.php',
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => [
// ... other config
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively override configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => [
realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local,{,*.}deploy,{,*.}development}.php',
],
// ... other config
],
Any of the *.local.php or *.global.php config files should be placed in /config/autoload/. Though, if you modify the above config, you could technically place it wherever you'd like.
Next, make sure you have the Zend\Db module enabled in your configuration. Open up the /config/modules.config.php file and make sure Zend\Db is in the list of Zend modules to be loaded in.
Lastly, you have not provided the config you used, so I'm assuming you made a mistake there. Use something like the config below in your /config/autoload/database.local.php file. Technically you could split this over 2 files; a global and a local file. Local files are (/should) not be committed into version control history and as such can contain usernames and passwords. Any other config, such as using Pdo for a driver could go into global config.
'db' => [
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf3;hostname=localhost',
'username' => 'root',
'password' => 'root',
'driver_options' => [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
],
],

Is it possible CookieStore config not working correctly?

Even if I configure CookieStore with:
MyApp::Application.config.session_store :cookie_store, {
key: '_myapp_session',
cookie_only: false,
httponly: false
}
and I make a POST request with
_myapp_session = #SOME_SESSION_ID
and authenticity_token = #AUTH_TOKEN
and http-header[X-CSRF-Token] set to #AUTH_TOKEN
finally the user is not authenticated, and new session is created with new AUTH_TOKEN and session id.
Can anybody give me some suggestions ?
Unfortunately there is NO SUPPORT for this option in current 4.0 branch. Although this option is forced to TRUE and I didn't find any code in rails sources using this option.
The way to make isolated POST's requests working is to create dedicated middleware, which will add appropriate session cookie. It must be inserted in the apropriate place, because middleware's order is significant.
Because it is very dangerous in CSRF security aspect, we must add to our middleware same verification, which will limit potential risk to minimum.

How to run two versions of static website simultaneously for A/B testing?

For example I have a very simple website only HTML/CSS/Javascript based no php,.net, ruby is being used. Some visitors to my site should see version “A,” while others see version “B.
Site is hosted on Apache server
One way to do that is to run Apache as a load balancer, "balancing" between the two different versions of the site (which are their own vhosts running on other ports). When someone connects, apache will randomly direct them to version A or version B of the site. You can use mod_proxy_balancer to do this. It also supports "sticky" balancing: that is, you can set a cookie so that once a client gets into version A, they're always directed to version A (and likewise with version B).
Sounds like you might want .htaccess:
http://httpd.apache.org/docs/2.2/mod/mod_alias.html
http://www.webweaver.nu/html-tips/web-redirection.shtml
One way is by analyzing the IP Address or range.
Create list of IP Address/Range
$ips = array(
"a" => array( 'xxx.xxx.xxx.xxx', ....),
"b" => array( 'xxx.xxx.xxx.xxx', ....),
);
Now, use $_SERVER['REMOTE_ADDR'] and switch the domain
if(in_array($ips['a'], $_SERVER['REMOTE_ADDR'])) {
header("location: website.com/a");
} else {
header("location: website.com/b");
}
exit;