Yii2 Links between Frontend and Backend - yii2-advanced-app

how can I switch from frontend to backend using links in yii2 application? Actually, I only can set links between frontend or backend, not from frontend to backend.I suppose, I have to change my config-file(main-local.php/urlManager) So, here it is
.
.
.'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => true,
'enableStrictParsing' => true,
'rules' => [
'/' => 'site/login',
'home' => 'site/index',
'reset' => 'site/request-password-reset',
'about' => 'site/about',
'contact' => 'site/contact',
'logout' => 'site/logout',
'signup' => 'site/signup',
'gii' => '/gii',
'debugger' => '/debug',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<id:\d+>' => '<controller>/save-as-new',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<action:(contact|captcha)>' => 'site/<action>',
],
.
.
.

Create another url manager in frontend and set the baseurl to direct it to backend and add your backend rules if any. And use the created component for creating backend urls
'urlManagerBackend' => [
'baseUrl' => str_replace('/frontend/web', '/backend/web', (new Request)->getBaseUrl()),
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
And vice-versa for backend to frontend
*and yes also include this in config
use \yii\web\Request;

Related

URL-Manager will not route

I cloned our repositorie and created excatly the same URL-rules like at my own project. Now, i will get error like this after having logged in:
Firefox:
Fehler: Umleitungsfehler
The website called is rerouting request,which never will come to an end.
This problem sometimes occures, if cookies are deactivated
Chrome:
ERR_TOO_MANY_REDIRECTS
I definetly accepted using cookies at both browser!!
Debugging shows me,that I have dozen of 302-Requests,so Yii breaks down!
I use Windows, not LINUX, so I don't care about any permissions.
Here are the rules:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => true,
'enableStrictParsing' => true,
'rules' => [
'/' => 'site/login',
'home' => 'site/index',
'logout' => 'site/logout',
'contact' => 'site/contact',
'signup' => 'site/signup',
'reset' => 'site/request-password-reset',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<action:(contact|captcha)>' => 'site/<action>'
],
Here is frontend configuration:
<?php
$config = [
// LZA 17-07-30
'sourceLanguage' => 'de-DE',
'language' => 'de-DE',
// LZA 17-07-30 siehe Funktionen in http://demos.krajee.com/grid#module
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '4lD2RxDNkC4ckpwxTmkDzOLIvk0JMs3F',
],
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
// LZA 17-07-30 CRUD
'generators' => [// customized CRUD generator
'crud' => [
// 'class' => 'app\myCrud\crud\Generator', // LZA 17-07-20 die Klasse von CRUD generator
'class' => '\common\wsl_dev\wsl_crud\crud\Generator', // LZA 17-07-20 die Klasse von CRUD generator
'templates' => [
'myCrud' => '/#common/wsl_dev/wsl_crud/crud/default', //LZA 17-07-20 Templatename und Templatepfad
]
]
],
// LZA 17-07-30 CRUD
];
}
return $config;
If I deacitvate URLManger,setting
'enablePrettyUrl' => false,
everything works fine.
If I put in manually Url like this:
http://localhost/yii2_perswitch/frontend/web/yiic.php/home
everything works fine,too
Any ideas,how to fix this?
I deleted all my cookies,without any effects!
Solution 1:
Enable debug mode from web/index.php (uncomment these two lines):
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
and you can see exactly what causes the problem.
Redirected too many times error was because the 777 permissions for
runtime and assets folder were not set.
Solution 2:
I think that the problem is related with the path or domain of the cookie. I believe that this info could be useful.
https://github.com/samdark/yii2-cookbook/blob/master/book/cookies.md

SQLSTATE[HY000] [1045] Access denied for user - OVH Eloquent

For a project I use Slim 3 - Twig and Eloquent. On development mode, all works perfectly, but on production, hosted in the OVH shared server, I can't access to the database.
I'm hundred percent sure of credentials and the database was not created right now.
This is my code :
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($config['db']);
$capsule->bootEloquent();
$capsule->setAsGlobal();
Where $config['db'] contains the informations required by Eloquent :
$config = [
'settings' => [
'debug' => true,
'displayErrorDetails' => true
],
'db' => [
'driver' => 'mysql',
'host' => '****.mysql.db',
'database' => '****',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]
]
What I have to do to make it work ?
Ok ... I get the solution.
... Suspense ...
I have to remove spaces around the big arrows in my beautiful formatted array.
Yes. I'm gonna cry.

Hide controller and action in pretty URL with Yii2

I need to change a URL in Yii2 using the URL Manager from
http://www.domain.com/index.php?r=tamil-article/articles&categ=Innovation&id=44
to
http://www.domain.com/44/Innovation.html
How can this be done?
You can resolve this by configuring your UrlManager to use prettyUrls.
After that you can add a custom url rule to the rules array (in config/main.php):
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Add the .html suffix
'suffix' => '.html',
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => [
'<id:\d+>/<categ:\w+>' => 'tamil-article/articles',
],
],

How can I hide create new button in TYPO3 TCA type inline?

I would like to hide "create new" image button in case of TCA for field type is inline.
My code is below:
<pre>
'image' => array(
'label' => 'Image',
'config' => array(
'type' => 'inline',
'foreign_table' => 'sys_file_reference',
'foreign_field' => 'uid_foreign',
'foreign_sortby' => 'sorting_foreign',
'foreign_table_field' => 'tablenames',
'foreign_match_fields' => array(
'fieldname' => 'field_slide_image',
),
'foreign_label' => 'uid_local',
'foreign_selector' => 'uid_local',
'foreign_selector_fieldTcaOverride' => array(
'config' => array(
'appearance' => array(
'elementBrowserType' => 'file',
'elementBrowserAllowed' => $allowedFileExtensions
)
)
),
'filter' => array(
array(
'userFunc' => 'TYPO3\\CMS\\Core\\Resource\\Filter\\FileExtensionFilter->filterInlineChildren',
'parameters' => array(
'allowedFileExtensions' => $allowedFileExtensions,
'disallowedFileExtensions' => $disallowedFileExtensions
)
)
),
'appearance' => array(
'useSortable' => TRUE,
'headerThumbnail' => array(
'field' => 'uid_local',
'width' => '45',
'height' => '45c',
),
'showPossibleLocalizationRecords' => FALSE,
'showRemovedLocalizationRecords' => FALSE,
'showSynchronizationLink' => FALSE,
'showAllLocalizationLink' => FALSE,
'showPossibleRecordsSelector' => "hide",
'enabledControls' => array(
'info' => FALSE,
'new' => false,
'dragdrop' => TRUE,
'sort' => true,
'hide' => TRUE,
'delete' => TRUE,
'localize' => TRUE,
),
),
'behaviour' => array(
'localizationMode' => 'select',
'localizeChildrenAtParentLocalization' => TRUE,
),
),
)
</pre>
I have added this code 'new' => false, but still it is not working.
Found Solution :
I have found one solution https://forge.typo3.org/issues/71918
I hope this can help for other users.
Use the permission system of TYPO3 to only allow read access to the field for a certain user group.
Hide "New" Button in TCA with
['appearance']['enabledControls']['new'] = false
This works in 8.x only.
Found Solution :
I have found one solution https://forge.typo3.org/issues/71918
// Render the level links (create new record):
if ($config['appearance']['enabledControls']['new']) {
> $levelLinks = $this->getLevelInteractionLink('newRecord', $nameObject . '-' . $foreign_table, $config);
}
Afaik, the "new" button only becomes hidden if the "maxitems" limit is set and reached.

Yii mongodbsuite two and more database connections

How to make two or more database connections in Yii mongodbsuite?
I added 2 DB components in main.php:
'components' => array(
'mongodb' => array(
'class' => 'common\extensions\MongoDB',
'connectionString' => 'mongodb://localhost:27017/table1',
'dbName' => 'table1',
'fsyncFlag' => false,
'persistentConnection' => 'x',
'replicaSet' => false,
'safeFlag' => true,
'useCursor' => false,
),
'mongodb2' => array(
'class' => 'common\extensions\MongoDB',
'connectionString' => 'mongodb://localhost:27017/table2',
'dbName' => 'table2',
'fsyncFlag' => false,
'persistentConnection' => 'x',
'replicaSet' => false,
'safeFlag' => true,
'useCursor' => false,
),
)
But how to use mongodb2 in query (ex: ->findByAttributes()) i don't know.
Please provide some example queries using the mongodb2 connection above.
I just add 'mongodb2' => array to 'components' => array and add to models (ex: User) who extends EMongoDocument:
public function getMongoDBComponent() {return Yii::app()->mongodb2;}
And it is work!
Have a look at Multiple db support in Yii.
Basically you have to modify your active record class by overriding the getDbConnection() methods. (In the given link you have a better example using an intermediate inheritance layer)