PHP code example of nette / http

1. Go to this page and download the library: Download nette/http 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/ */

    

nette / http example snippets


$url = $httpRequest->getUrl();
echo $url; // https://nette.org/en/documentation?action=edit
echo $url->getHost(); // nette.org

$all = $httpRequest->getQuery();    // array of all URL parameters
$id = $httpRequest->getQuery('id'); // returns GET parameter 'id' (or null)

$all = $httpRequest->getPost();     // array of all POST parameters
$id = $httpRequest->getPost('id');  // returns POST parameter 'id' (or null)

$file = $httpRequest->getFile('avatar');
if ($file->hasFile()) { // was any file uploaded?
	$file->getName(); // name of the file sent by user
	$file->getSanitizedName(); // the name without dangerous characters
}

$files = $httpRequest->getFiles();

$sessId = $httpRequest->getCookie('sess_id');

$cookies = $httpRequest->getCookies();

echo $httpRequest->getMethod(); // GET, POST, HEAD, PUT

if ($httpRequest->isMethod('GET')) ...

$userAgent = $httpRequest->getHeader('User-Agent');

$headers = $httpRequest->getHeaders();
echo $headers['Content-Type'];

$body = $httpRequest->getRawBody();

// Header sent by browser: Accept-Language: cs,en-us;q=0.8,en;q=0.5,sl;q=0.3

$langs = ['hu', 'pl', 'en']; // languages supported in application
echo $httpRequest->detectLanguage($langs); // en

$factory = new Nette\Http\RequestFactory;
$httpRequest = $factory->fromGlobals();

// remove spaces from path
$requestFactory->urlFilters['path']['%20'] = '';

// remove dot, comma or right parenthesis form the end of the URL
$requestFactory->urlFilters['url']['[.,)]$'] = '';

// clean the path from duplicated slashes (default filter)
$requestFactory->urlFilters['path']['/{2,}'] = '/';

$httpResponse->setCode(Nette\Http\Response::S404_NotFound);

$httpResponse->setHeader('Pragma', 'no-cache');

$httpResponse->addHeader('Accept', 'application/json');
$httpResponse->addHeader('Accept', 'application/xml');

$pragma = $httpResponse->getHeader('Pragma');

$headers = $httpResponse->getHeaders();
echo $headers['Pragma'];

$httpResponse->setContentType('text/plain', 'UTF-8');

$httpResponse->redirect('http://example.com');
exit;

// browser cache expires in one hour
$httpResponse->setExpiration('1 hour');

$httpResponse->setCookie('lang', 'en', '100 days');

$httpResponse->deleteCookie('lang');

[
	'avatar' => /* FileUpload instance */
]

$request->getFile('avatar')->hasFile();

[
	'my-form' => [
		'details' => [
			'avatar' => /* FileUpload instance */
		],
	],
]

[
	'my-form' => [
		'details' => [
			'avatars' => [
				0 => /* FileUpload instance */,
				1 => /* FileUpload instance */,
				2 => /* FileUpload instance */,
			],
		],
	],
]

$file = Nette\Utils\Arrays::get(
	$request->getFiles(),
	['my-form', 'details', 'avatars', 1],
	null
);
if ($file instanceof FileUpload) {
	...
}

$file->move('/path/to/files/name.ext');

$section = $session->getSession('unique name');

// $this is Presenter
$section = $this->getSession('unique name');

// variable writing
$section->userName = 'john'; // nebo $section['userName'] = 'john';

// variable reading
echo $section->userName; // nebo echo $section['userName'];

// variable removing
unset($section->userName);  // unset($section['userName']);

foreach ($section as $key => $val) {
	echo "$key = $val";
}

$section->warnOnUndefined = true;

// section will expire after 20 minutes
$section->setExpiration('20 minutes');

// variable $section->flash will expire after 30 seconds
$section->setExpiration('30 seconds', 'flash');

use Nette\Http\Url;

$url = new Url;
$url->setScheme('https')
	->setHost('localhost')
	->setPath('/edit')
	->setQueryParameter('foo', 'bar');

echo $url; // 'https://localhost/edit?foo=bar'

$url = new Url(
	'http://john:xyz%[email protected]:8080/en/download?name=param#footer'
);

echo $url;
echo json_encode([$url]);

$url->isEqual('https://nette.org');

use Nette\Http\UrlImmutable;

$url = new UrlImmutable(
	'http://john:xyz%[email protected]:8080/en/download?name=param#footer'
);

$newUrl = $url
	->withUser('')
	->withPassword('')
	->withPath('/cs/');

echo $newUrl; // 'http://nette.org:8080/cs/?name=param#footer'

echo $url;
echo json_encode([$url]);