PHP code example of truecastdesign / true

1. Go to this page and download the library: Download truecastdesign/true library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

truecastdesign / true example snippets





define('BP', __DIR__);

ecastdesign/true/src/Exceptions.php';

$App = new True\App;
$App->load('site.ini');

if ($App->config->site->debug) {
	$GLOBALS['debug'] = true;
	error_reporting(E_ALL & ~E_NOTICE);
} else {
	$GLOBALS['debug'] = false;
	error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
}

$App->request = new True\Request;
$App->response = new True\Response;
$App->router = new True\Router($App->request);
$App->view = new True\PhpView;

# global css and js files
$App->view->css = '/vendor/truecastdesign/true/assets/default.css, /assets/css/site.css'; # global css files
# You can also add them on separate lines or different places in your code
$App->view->css = '/assets/example1.css';
$App->view->css = '/assets/example2.css';
# both will be combined and minified. This works for JS files as well.

$App->view->js = '/assets/js/file1.js, /assets/js/file2.js'; # global js files

# If you need to pass variables to the layout template or page template globally, you can use the variables key on the PhpView object as an key value array item.
$App->view->variables = ['key'=>'value'];
$App->view->variables = ['key2'=>'value2'];
$App->view->variables = ['key'=>'value', 'key3'=>'value3'];
# all the arrays will get merged together so you can keep added them and they will all be available. In the view: <?=$key

$App->router->get('/api/user/*:id', function($request){
	echo $request->route->id;
});

if ($request->files->file->uploaded) {
	// do something like move or resize it
	echo $request->files->file->ext; // jpg
	echo $request->files->file->mime; // image/jpeg
	echo $request->files->file->tmp_name; // /path/j23k4j8d
}

$request->files->file->imageWidth = 800;
$request->files->file->imageHeight = 800;
try {
	$request->files->file->resize();
} catch (Exception $e) {
	echo $e->getMessage();
}

try {
	$request->files->file->crop([0,20,0,0]);
} catch (Exception $e) {
	echo $e->getMessage();
}

try {
	$request->files->file->cropSquare();
} catch (Exception $e) {
	echo $e->getMessage();
}

try {
	$request->files->file->cropBottomSquare();
} catch (Exception $e) {
	echo $e->getMessage();
}

try {
	$request->files->file->cropTopSquare();
} catch (Exception $e) {
	echo $e->getMessage();
}

try {
	$request->files->file->move('/path/', 'newFileName.'.$request->files->file->ext);
} catch (Exception $e) {
	echo $e->getMessage();
}

$App->response('{"result":"success"}', 'json', 200, ["Cache-Control: no-cache"]);

try {
	// run method
} catch (Exception $e) {
	$App->response('{"result":"error", "error":"'.$e->getMessage().'"}', 'json', 401);
}
$App->response('{"result":"success"}', 'json'); // this will run if the above 
// response does not but will not run if there was already a response run above.

$App->any('/*:path', function($request) use ($App) {
	$vars = []; 
	@['selectedNav'] = (object) [$request->route->path => ' class="sel"'];
	
	$App->view->render($request->route->path.'.phtml', $vars);
});

$App->view = new True\PhpView;

$App->view->css = '/assets/css/site.css';

$App->view->js = '/assets/js/testing.js, /assets/js/testing2.js';

$App->view->cache = true; 

$App->view->title = "Title Tag";
$App->view->description = "This is the meta description and og:description";
$App->view->canonical = "http://www.domain.com/canonical-url";
$App->view->linkText = "Clickable Text to link to page"; 
$App->view->headHtml = "<script src='/path/js/dom/js'></script>"; 
$App->view->html // page body HTML 
$App->view->breadcrumbs // array of breadcrumbs with name and url
// populate using meta area of view file like the following. Only on pages with parent pages. Home is not a parent page according to Google. Put them in decending order from parents down.
// breadcrumb[] = "Top Parent Title|/parent"
// breadcrumb[] = "Next Parent Title|/parent/other-parent"

$App->view->created = "2022-12-01" // set the date the page was created. It can be outputted or used by calling $App->view->created. If you want a different date format, call $App->view->created("M d, Y") can pass the PHP date formatting string you want.

$App->view->modified = "2022-12-01" // You can set the page or article modified date if you want full control of it. This variable will also be auto filled if not provided from the modified date of the view file. Just like the created variable, you can format it by calling $App->view->modified("M d, Y");

customVariable = "custom value"
{endmeta}

<?if ($App->view->isset('customVariable')):

$App->view->render('page.phtml', ['var1'=>6]);

$App->view->layout = BP."/app/views/_layouts/landing-page.phtml";
$App->view->render('page.phtml');

$App->view->render(BP.'/vendor/truecastdesign/trueadmin/views/not-authorized.phtml');

$App->view->error(404); // other errors supported: 301, 302, 303, 304, 307, 308, 400, 401, 403, 404, 405

$App->view->403 = 'not-authorized.phtml';

$mail = new \True\Email('domain.com', 587, 'tls', 'login');  // ssl and tls are turned on or off automatacally based on the port provided.
$mail->setLogin('[email protected]', 'password')
->setFrom('[email protected]', 'name')
->addReplyTo('[email protected]', 'name')
->addTo('[email protected]', 'name')
->addCc('[email protected]', 'name')
->addBcc('[email protected]', 'name')
->addAttachment(BP.'/path/to/filename.jpg')
->addHeader('header-title', 'header value')
->setCharset('utf-16', 'header value') // default: utf-8;  values: utf-16, utf-32, ascii, iso 8859-1 
->setSubject('Test subject')
->setTextMessage('Plain text message')
->setHtmlMessage('<strong>HTML Text Message</strong>')
->setHTMLMessageVariables(['name'=>'John Doe', 'phone'=>'541-555-5555', 'message'=>'Plain text message'])
->addHeader('X-Auto-Response-Suppress', 'All');

if ($mail->send()) {
	echo 'SMTP Email has been sent' . PHP_EOL;   
} else {
	echo 'An error has occurred. Please check the logs below:' . PHP_EOL;
	pr($mail->getLogs());
}

$Users = new Users($DB);
$LoginAttempts = new True\LoginAttempts($DB);
$JWT = new True\JWT;
$PasswordGenerator = new True\PasswordGenerator;

$taAuthConfig = $App->getConfig('trueadminAuth.ini');

$Auth = new True\AuthenticationJWT($Users, $LoginAttempts, $JWT, $PasswordGenerator, $App, [
	'privateKeyFile'=>BP.'/app/data/key_private_rsa.pem', 
	'publicKeyFile'=>BP.'/app/data/key_public_rsa.pem', 
	'encryptionPasswordFile'=>'trueadminAuth.ini', 
	'pemkeyPassword'=>$taAuthConfig->pemkey_password, 
	'https'=>$taAuthConfig->https,
	'ttl'=>$taAuthConfig->pemkey_password,
	'alg'=>$taAuthConfig->alg
]);

try {
	if ($Auth->isLoggedIn()) {
		# user logged in
	} 
} catch (Exception $e) {
	echo $e->getMessage();
}

$logFile = BP."/logs/access.log";

$Parser = new True\LogParser($logFile);

foreach ($Parser as $row) {
	print_r($row);
}

$schemaInfo = (object)[
	"base_url"=>$App->config->site->url,
	"url"=>$App->request->url->full, 
	"title"=>$App->view->title, 
	"description"=>$App->view->description, 
	"site_logo_url"=>$App->config->site->site_logo_url, 
	"site_logo_width"=>$App->config->site->site_logo_width, 
	"site_logo_height"=>$App->config->site->site_logo_height, 
	"site_logo_caption"=>$App->config->site->site_logo_caption, 
	"datePublished"=>$App->view->datePublished, 
	"dateModified"=>$App->view->dateModified, 
	"social_media"=>$App->config->social_media
];

echo $SEO->schemaGraph($schemaInfo);

<?=$SEO->jsonLD('article', [
	'datePublished'=>'2024-01-01',
	'headline':"The great article"
]);

$eventData = [
	'orderNumber'=>1254875,
	'total'=>'$25.80',
	'source'=>'BatteryStuff',
	'coupon'=>"ZJUE",
	'shipping'=>2.80,
	'tax'=>0.50,
	'items'=>[
		[
			'partNumber'=>'rx8',
			'name'=>'Battery Charger RX8',
			'coupon'=>'ZJUE',
			'discount'=>0.30,
			'brand'=>"Chargers Unlimited",
			'category'=>'Battery Chargers > 12 Volt > Single Bank > 5-10 Amps',
			'variant'=>'With Cables',
			'price'=>'$7.80',
			'quantity'=>'1'
		],
		[
			'partNumber'=>'rx9',
			'name'=>'Battery Charger RX9',
			'coupon'=>'JEN',
			'brand'=>"Chargers Unlimited",
			'category'=>'Battery Chargers > 12 Volt > Single Bank > 10-20 Amps',
			'price'=>7.90,
			'quantity'=>2
		]
	]
];
$GoogleTagManager = new True\GoogleTagManager;
echo $GoogleTagManager->event('purchase', $eventData);
comf
AddHandler application/x-httpd-php .html .phtml .php

<IfModule mod_rewrite.c>
	RewriteEngine On

	RewriteBase /

	RewriteRule .? - [E=HEADER>Authorization:%{HTTP:Authorization}]
	
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^ index.php [QSA,L]
</IfModule>
html
<?=$App->view->headHtml
html
<?=$App->view->cssoutput
html
<?=$App->view->jsoutput
shell
% phpunit tests/NonceTest.php