BCS Yii2 Skeleton

The basic web site installation should be modified to enable pretty url’s.

Modify config web.php to turn pretty url’s on.

'components' => [
    // ...
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Hide index.php
        'showScriptName' => false,
        // Use pretty URLs
        'enablePrettyUrl' => true,
        'rules' => [
        ],
    ],
    // ...
],

The next step is to place a .httpaccess file in the web directory.

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

You should also update the cookie key in the config web.php file.

<?php
 
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
 
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'AMBSecretKey',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
 
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
 
    ],
    'params' => $params,
];
 
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
 
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}
 
return $config;

To complete the basic modifications change all references from “My Company” to something more suitable in the main.php file.

Below is the main.php with all the occurrences changed.

<?php
 
/* @var $this \yii\web\View */
/* @var $content string */
 
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
 
AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
 
<div class="wrap">
    <?php
    NavBar::begin([
        'brandLabel' => 'Brooks Computing Systems, LLC',
        'brandUrl' => Yii::$app->homeUrl,
        'options' => [
            'class' => 'navbar-inverse navbar-fixed-top',
        ],
    ]);
    echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => [
            ['label' => 'Home', 'url' => ['/site/index']],
            ['label' => 'About', 'url' => ['/site/about']],
            ['label' => 'Contact', 'url' => ['/site/contact']],
            Yii::$app->user->isGuest ? (
                ['label' => 'Login', 'url' => ['/site/login']]
            ) : (
                '<li>'
                . Html::beginForm(['/site/logout'], 'post')
                . Html::submitButton(
                    'Logout (' . Yii::$app->user->identity->username . ')',
                    ['class' => 'btn btn-link logout']
                )
                . Html::endForm()
                . '</li>'
            )
        ],
    ]);
    NavBar::end();
    ?>
 
    <div class="container">
        <?= Breadcrumbs::widget([
            'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
        ]) ?>
        <?= $content ?>
    </div>
</div>
 
<footer class="footer">
    <div class="container">
        <p class="pull-left">&copy; Brooks Computing Systems, LLC <?= date('Y') ?></p>
 
        <p class="pull-right"><?= Yii::powered() ?></p>
    </div>
</footer>
 
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

These modifications should supply a reasonable base line for subsequent Yii2 development

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Install Tomcat 9 Mint

The web application for Java and Grails is the Apache Tomcat Server 9.

Step 1. Verify Java

/opt/tomcat9/webapps/host-manager/META-INF

Step 2. Get the server.

cd /opt
sudo wget http://www.us.apache.org/dist/tomcat/tomcat-9/v9.0.0.M22/bin/apache-tomcat-9.0.0.M22.tar.gz

Below observe the result of the wget command.

Next we must unzip Tomcat 9 and move the extracted sub directory to its permanent location.

Issue the following commands to facilitate installation.

sudo tar xzf apache-tomcat-9.0.0.M22.tar.gz
sudo mv apache-tomcat-9.0.0.M22 tomcat9

Below you can observe each command execution followed by the dir command to reveal the contents of the directory.

At this point we are ready to start the server by issuing the following two commands.

cd /opt/tomcat9/bin
./startup.sh

The screen below illustrates the tomcat startup script.

Now issue the following command at the browser http://localhost:8080 and the following screen will appear.

The next step provides credentials for Tomcat Managers.

<user username="tomcat" password="secretPassword" roles="admin-gui,manager-gui"/>

Make sure you have this entry in the tomcat-users.xml file.

By default if Tomcat (version 9) is loaded on a server you use a client to access the interface but additional configuration is required.

Two files must be modified to relax the default restriction.  Both files are named context.xml.

The first location is /opt/tomcat9/webapps/host-manager/META-INF.  The second location is /opt/tomcat9/webapps/manager/META-INF.

<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"/>

This value should be commented out as indicated below.

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"/> -->

You should be able to access all components of Tomcat (version 9) at this point without restrictions.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

BCS Java Date Difference

There are may solution to Java challenges. Today our challenge surrounds calculating elapsed years, months, days, hours, minuets and seconds between two dates.

The primary workhorse for this task is JodaTime utility. Click on the Releases option and the Downloads sub option to download jars and utilities. Simply add the downloaded jar to tour Java project and you are ready for action.

package dtt;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.joda.time.Interval;
import org.joda.time.Period;
 
public class ft {
 
	public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) {
 
		1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat simpleDateFormat = new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("dd/M/yyyy hh:mm:ss");
 
		try {
 
			5+0%2Fdocs%2Fapi+Date">Date date1 = simpleDateFormat.parse("10/10/2016 11:30:10");
			5+0%2Fdocs%2Fapi+Date">Date date2 = simpleDateFormat.parse("13/11/2017 20:35:55");
 
			// obj.printDifference(date1, date2);
			printDifference(date1, date2);
 
		} catch (1.5.0/docs/api/java/text/ParseException.html">ParseException e) {
			e.printStackTrace();
		}
	}
 
	public static void printDifference(5+0%2Fdocs%2Fapi+Date">Date startDate, 5+0%2Fdocs%2Fapi+Date">Date endDate) {
 
		Interval interval = new Interval(startDate.getTime(), endDate.getTime());
		Period period = interval.toPeriod();
 
		1.5.0/docs/api/java/lang/System.html">System.out.println("  Start Date : " + startDate.toString());
		1.5.0/docs/api/java/lang/System.html">System.out.println("    End Date : " + endDate.toString());
		1.5.0/docs/api/java/lang/System.html">System.out.printf("Elapsed Time : " + "%d years, %d months, %d days, %d hours, %d minutes, %d seconds%n", period.getYears(),
				period.getMonths(), period.getDays(), period.getHours(), period.getMinutes(), period.getSeconds());
	}
}

The execution of the program is listed below.

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.