In Console app createAbsoluteUrl does not work. Do I need to change some setting in config file to made it work.
As per my understanding the request component is not available in Console app. So where do I specify the base URL.
I did not find anything on this. Any help would be appreciated. Thank you.
You are unable to create URL in console application while you will receive the following error:
'Please configure UrlManager::scriptUrl correctly as you are running a console application.'
To get rid of this message and work with UrlManager, in your console.php config file, add the following configuration:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'scriptUrl' => 'http://path/to'
]
Now, if you write the following lines in your console app:
echo \Yii::$app->getUrlManager()->createAbsoluteUrl('test/test');
echo "\n";
echo \yii\helpers\Url::toRoute('test/test');
You will see the following output:
http://path/to?r=test%2Ftest
http://path/to?r=test%2Ftest
Related
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\''
],
],
I'm using CakeDC's Users Plugin for authentication.
I've followed all the steps of installation as documented here, but i'm getting this error:
I've performed the following steps:
composer require cakedc/users
composer require league/oauth2-facebook:#stable
composer require league/oauth2-google:#stable
bin/cake migrations migrate -p CakeDC/Users
In config/bootstrap.php
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true ]);
Configure::write('Users.Social.login', true); //to enable social login
Load the Component in your src/Controller/AppController.php, and use
the passed Component configuration to customize the Users Plugin:
$this->loadComponent('CakeDC/Users.UsersAuth');
Update: I've removed
Configure::write('Users.config', ['users']);
this line from my bootstrap as i'm using the default users.php file which is present inside the plugin now.
But i get this error now:
Invalid provider or missing class (League\OAuth2\Client\Provider\LinkedIn)
I can get rid of this error by disabling social login (which is not what i want to do):
Configure::write('Users.Social.login', false);
After disabling the social login i get this error:
Error: A route matching "array ( 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'login', 'prefix' => false, '_ext' => NULL, )" could not be found.
Any help would save my day.
#ndm was right, Step 5 is for Configuration, when installing with composer removing the following 2 lines fixes the error before the Migration Step:
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true ]);
Then after the migration add these lines back in to continue the installation.
Step number 5 is customization - check the header in the docs that you've linked. So that's not actually a required installation step.
The problem should be rather easy to figure from the naming, the error that you're encountering, and the stacktrace (just check what's being done in each of the frames) - setting Users.config is to be used to define custom config files, the one you've defined doesn't exist (or isn't reabable), hence the error.
collection((array)Configure::read('Users.config'))->each(function ($file) {
Configure::load($file);
});
https://github.com/CakeDC/users/blob/3.2.0/config/bootstrap.php#L21-L23
So either don't do that and define the configuration elsewhere (being it in your app config, or wherever), or create the missing users.php config file and put your users plugin configuration in there.
See also
https://github.com/CakeDC/users/.../Configuration.md#overriding-the-default-configuration
myappI have to reply with a file in cakePHP3 and in a particular case, it seems that I have a problem, maybe of header.
I want to send an APK (Android package) file.
So if I directly access to the file online, there is no problem but I want to send it from my controller and the file seems to not be considered as a 'raw' apk file.
Here is my code:
public function getmyappapk()
{
$this->request->allowMethod([
'get'
]);
$this->response->type('application/apk');
$this->response->file(WWW_ROOT . DS. 'tmp' .DS. 'app-release.apk',
['download' => true, 'name' => 'MyApp.apk']);
return $this->response;
}
so as I said, if I access to www.mywebsite.com/tmp/app-release.apk, from my smartphone, it downloads it and I can install it.
But if I access to www.mywebsite.com/mycontroller/getmyappapk, it downloads the file and then, tells me that it's impossible to open it.
The type 'application/apk' seems to be ok because if I enter the url in a browser it tells me to record the file recognised as 'android packet'.
But something is certainly missing here.
What is the problem?
Error:
Unable to verify your data submission error
Create one public function in Yii2. But, submit data not accept in this method,
see following error images. Why is this happen?
If you create the form manually (i.e. without using the yii form methods), you should add an input field like this:
<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
source: http://zero-exception.blogspot.com/2015/01/yii2-using-csrf-token.html
Add this in the head section of your layout: <?= Html::csrfMetaTags() ?>
There are various solutions to this problem, I guess understanding why it happened in the first place helps solve the problem.
It is either caused by an invalid csrf token,user generated csrf token, expired csrf token, all these 'csrf' issues will arise when you have custom forms built not using the 'ActiveForm' class.
If CSRF is not the issue,
Its other cause occurs when you try to upload multiple files and the web server cannot handle the load. Some properties to check on this to make sure you are on the right track is to claculate the size of files one is attempting to upload and check the web server's post_max_size and upload_max_filesize
If you are running linux, check php.ini file for inputs like these:
; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size = 8M
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M
Based on your calculations, adjust the above php.ini parameters to suite your needs, then test.
I had a similar problem and I solved it.
To permanently disable csrf validation in whole application add below code in your configurations.
$config = [
'components' => [
'request' => [
'enableCsrfValidation' => false,
],
],
];
Also sometimes you can get this error using Yii 2.0 due to the post_max_size, upload_max_filesize, max_input_time also too maybe the webserver can be trimming the post so verify on nginx - client_max_body_size or in apache - LimitRequestBody
Disable for only specific action
/**
* #inheritdoc
*/
public function beforeAction($action)
{
if ($action->id == 'my-action') {
$this->enableCsrfValidation = false;
}
return parent::beforeAction($action);
}
Check whether there is hidden input in your form with CSRF token. It should look like
<input type="hidden" name="_csrf" value="ZEZ6Y0xrY3ARGS42fTwhMQgkDgF6BCEGEx4SMXQMBR4CPy0iPCIwNQ==">
A long story has been discussed here github
So disabling csrf somehow unsure for ajax request. I have met this issue many times.
So remember to send _csrf key when you send data by POST via ajax.
Updated from yii 2.0.12 to 2.0.30
Have this bad request error 400
Got the Html::csrfMetaTags() in the header layout.
using activeform,therefore is not caused by missing csrf token in the
form. And checked already through inspect.
I solved this through adding below code to the backend/config/main.php.
'components' => [
'request' => [
'csrfParam' => '_backend_csrf',
],
Is it a correct way or will it cause security issue?
A little differentiation to dchakarov's answer due to Yii2 tiers using instead of
_csrf variable _frontendCsrf.
<input type="hidden" name="_frontendCsrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
This is a second time this question did not help me even though I posted a comment previously, so I have to post a response.
$csrf = \yii::$app->request->csrfParam;
$token = \yii::$app->request->csrfToken;
//start from
echo Html::hiddenInput($csrf,$token);
//end from
Switched website form https to http and had the issue come up. Updated the request config by commenting the below lines to resolve.
'request' => [
'cookieValidationKey' => 'SomeRandomKeyValidationString',
//'csrfCookie' => [
// 'httpOnly' => true,
// 'secure' => true,
//],
Add this in your controller:
public $enableCsrfValidation = false;
I'm almost done with a Laravel project I'm working on and am wanting to try it out on an actual server.
However after I loaded the entire project (slower than using composer but I was hoping to keep this as simple as possible the first time I tried this) I can't even log in as I'm getting a "syntax error, unexpected '['" error message with the debug window pointing to this code:
Route::get('login', [
'as' => 'login',
'uses' => 'SessionsController#create'
]);
I tried changing it to
Route::get('login', array(
'as' => 'login',
'uses' => 'SessionsController#create'
));
but after I changed it and uploaded the file again it still looked like the original code. To make things more confusing the code should work either way, unless I am missing something.
If anyone can point out 1.)the reason for the error message - the project runs fine on my local server and 2.)why the file does not seem to be updating when I send in a new version it would be greatly appreciated! Thanks!
Your actual server is running PHP 5.3
Your local server is running PHP >=5.4
The short array syntax [] was added in PHP 5.4. See change log here.