Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts

Tuesday 26 March 2019

Yii first controller and URLs

This is partly based on the documentation found at https://www.yiiframework.com/doc/guide/2.0/en/start-hello
Within the controllers directory I have a file called SiteController.php. I'll explain a few things about it.

site is the default route for Yii applications and therefore SiteController.php would be (by default) the first controller a user interaction would access. So if I access my application through the URL http://localhost/test/yii-apps/basic/web/ then (by default) the SiteController would be called, and (by deafult) the actionIndex method would be called within that controller.

actionIndex renders the view views/site/index.php within the default layout views/layouts/main.php

In order to create a controller we need to make use of the controller classes declared at the top.

The method actionSay can be called from the URL http://localhost/test/yii-apps/basic/web/index.php
?r=site%2Fsay and within this URL, the r stands for route. route's format is ControllerID/ActionID. This would render the view views/site/say.php within the default layout views/layouts/main.php showing the content 'Hello' since this is the default value of the variable message.

The %2F is the URL encoded version of /

views/site/say.php looks like this:

<?php
use yii\helpers\Html;
echo Html::encode($message);
?>

To extend this approach the URL http://localhost/test/yii-apps/basic/web/index.php?r=site%2Fsay&message=Hello+Mick would pass a string Hello+Mick to the value of the variable message and the resulting page would display the string 'Hello Mick'.

Thus SiteController.php looks like this.

<?php
namespace app\controllers;
use yii\web\Controller;
class SiteController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionSay($message = 'Hello')
    {
        return $this->render('say', ['message' => $message]);
    }
}
?>

Wednesday 23 March 2016

Friday 26 April 2013

How to configure your Ubuntu localhost for PHP MVC URL routing

Step 1. Open a terminal and type
sudo gedit /etc/apache2/sites-available/default
or whatever text editor you like using to open up your apache configuration file for editing.
Step 2. Under the sections headed  <Directory /> and <Directory /var/www/>:
Change the line 'AllowOverride none' to 'AllowOverride All'.

Step 3.  Open up a terminal and type
hostname
This will display your hostname.

Step 4. Open up a terminal and type
sudo gedit /etc/hosts
or whatever text editor you like using to open up your hosts file for editing.
Step 5. Modify the first line so it reads
127.0.0.1    localhost localhost.localdomain yourhostname
Step 6. Open up a terminal and type
sudo a2enmod rewrite

Restart your computer.
It will now work.

Friday 2 March 2012

Removing cache manifest in Chrome

In an earlier post, I explained how to use cache-manifest for localstorage. Once you've achieved this, you'll find that in order to continue editing the site that the cache manifest will need to be removed. Otherwise, you won't see your changes. To do this in Chrome, you need to put this command in the address bar:
chrome://appcache-internals/
This will list all the sites in your application cache with a 'remove' link.
A full list of such commands, enter chrome://chrome-urls/

Friday 22 July 2011

Use PHP to get the current web address without the filename

Sometimes I need to grab the current web path without the current file name in order to pass a string in an email. As an example the current default location is http://www.effectivewebdesigns.co.uk/index.php, but in the future it may be http://www.effectivewebdesigns.co.uk/index.html or http://www.effectivewebdesigns.co.uk/default.html so sometimes I need to future proof.

<?php
$webAddress = 'http://'.$_SERVER['SERVER_NAME'];
$webAddress .= $_SERVER['REQUEST_URI'];
$webAddress = substr($webAddress, 0, 0-(strlen(basename($_SERVER['REQUEST_URI']))));
echo $webAddress;
?>

Friday 13 August 2010

Automatic menu selection using PHP

Suppose I have a website. In this case a PHP driven HTML5 website, although what I'm about to explain doesn't really require HTML5 and can be easily adapted.

See demo.

I have a menu. I have a text file called menu.txt which contains my menu items like this:

index.php,home
index2.php,Page 2
index3.php,Page 3

And what I want to do is call a PHP script which:

  1. Reads the file.
  2. Creates the menu tags from the file contents.
  3. Automatically selects the page I'm looking at as the selected item in the menu.


I can then apply CSS as I like to the menu appearance using a different colour for the selected item.

Here is how I do it. First place the include 'navigation.php'; in your page script where you want the navigation to appear.
This is what navigation.php looks like:

<nav>
<ul>
<?php
/* get the contents of menu.txt */
$fileArr = file('includes/menu.txt');
/* get the name of the PHP file referred to in the URL */
$fname = basename($_SERVER['REQUEST_URI']);
/* if there isn't a PHP file extension in the URL, it must be root. Therefore call it index.php */
if(substr($fname,-4) <> ".php")
{
$fname = "index.php";
}
/* create the menu from the lines in menu.txt */
foreach ($fileArr as $line)
{
$menuitems = explode(",", $line);
echo '<li><a href="'.$menuitems[0].'" ';
/* Check the current pathname in menu.txt with the one in the current URL */
if($menuitems[0] == $fname)
{
/* If they are the same add the menuSelected class to the link */
echo 'class="menuSelected"';
}
echo '>'.$menuitems[1].'</a></li>';
}
?>
</ul>
</nav>


Hey presto! Have fun.