Seo Friendly Urls If Enabled In Backend?

Hello!

I’ve enabled seo-friendly url’s for the front-end of a project. I want to be able to enable or disable this option in a page(view) in the backend. I’ve created an extra column in the table website in the database.

However i’m not able to get this working yet.

project/protected/config/main.php <-- Is where i’ve added the URL manager in the application components array, to have seo-friendly url’s in the frontend.

I thought it would be doable with an easy if else statement, however this doesn’t work in this case because its in the middle of an array. Any suggestions on how to do this?

project/protected/backend/views/options/index.php <-- Is the page where you should be able to enable/disable seo friendly urls by checking/unchecking a checkbox.

Can anyone help me fix this? I cannot get it working yet.

Thank you so much already!

There is no database connection yet when config/main.php is loaded (since the database info is in the config).

But you should be able to do it in onBeginRequest by adding this to config/main.php:




'onBeginRequest' => function (CEvent $event) {

    // If you have a model for your config table, you can also use that

    $friendlyUrls = Yii::app()->db

        ->createCommand('SELECT value FROM config WHERE key = "friendly_urls"')

        ->queryScalar();

    

    if ($friendlyUrls) {

        Yii::app()->urlManager->showScriptName = false;

        Yii::app()->urlManager->urlFormat = 'path';

    }

},