previewtechs/website-utilities

Website utilities based on PSR

v1.0 2017-10-04 22:48 UTC

This package is auto-updated.

Last update: 2024-12-06 10:20:52 UTC


README

composer require previewtechs/website-utilities

General robots.txt

<?php
require "vendor/autoload.php";

$rulesOne = new \Previewtechs\WebsiteUtilities\RobotsDotTxtGenerator\RobotsDotTxtRules('*');
$rulesOne->allow('/test')
    ->allow('/me')
    ->disallow('/thanks');

$rulesTwo = new \Previewtechs\WebsiteUtilities\RobotsDotTxtGenerator\RobotsDotTxtRules('GoogleBot');
$rulesTwo->allow('/test')
    ->allow('/me')
    ->disallow('/thanks');

$robotGenerator = new \Previewtechs\WebsiteUtilities\RobotsDotTxtGenerator\RobotsDotTxtGenerator();
$robotGenerator->addRules($rulesOne);
$robotGenerator->addRules($rulesTwo);

//To print directly
echo $robotGenerator;

//To send response directly
return $robotGenerator->respondAsTextFile(\Psr\Http\Message\ResponseInterface $response);

Output:

User-Agent: *
Allowed: /test
Allowed: /me
Disallow: /thanks

User-Agent: GoogleBot
Allowed: /test
Allowed: /me
Disallow: /thanks

Generate sitemap.xml

<?php
require "vendor/autoload.php";

$urls = [
    'https://site.com/test.php' => [
        'changefreq' => 'daily',
        'priority' => 1,
        'lastmod' => date('Y-m-d')
    ],
    'https://test.com/another_test.php' => [
        'changefreq' => 'daily',
        'priority' => 2,
        'lastmod' => date('Y-m-d')
    ],
];

$gen = new \Previewtechs\WebsiteUtilities\SitemapGenerator\SitemapGenerator();
$gen->loadUrls($urls);
//If you want to send xml response directly
return $gen->respondAsXML(\Psr\Http\Message\ResponseInterface $response);

//Or, if you want to just print the sitemap
echo $gen;

Output:

<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
			    http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
	<url>
		<loc>https://site.com/test.php</loc>
		<changefreq>daily</changefreq>
		<priority>1</priority>
		<lastmod>2017-09-30</lastmod>
	</url>
	<url>
		<loc>https://test.com/another_test.php</loc>
		<changefreq>daily</changefreq>
		<priority>2</priority>
		<lastmod>2017-09-30</lastmod>
	</url>
</urlset>

301 Redirect

<?php
require "vendor/autoload.php";

//Application wide middleware
$oldAndNewPathMapping = [
    '/oldPath1' => 'newPath1',
    '/oldPath2' => 'newPath2'
];
$threeZeroOneRedirect = new ThreeZeroOneRedirectPsrMiddleware();
$threeZeroOneRedirect->setPaths($oldAndNewPathMapping);
$app->add($threeZeroOneRedirect);
//End of 301 redirect middleware

Bug & Issues

Please submit issues, questions in GitHub Issues.

Contributors

Shaharia Azam from Preview Technologies Limited. To see all contributers click here