Generating a page from a PHP class

I really create websites for fun, and some of my friends told me that I could be more effective if I could create page output with a php class that would represent the whole page.

I was wondering how you would do it.

thanks

+3
source share
7 answers

I would suggest exploring some PHP frameworks, such as Cake, which is good for newbies, or Symfony or Zend Framework, if you are more qualified. This will greatly simplify your PHP development.

+1
source

OO.. , , / - .

class Page {
    public $meta_title;
    public $meta_keywords;
    public $html_body;

    public function displayPage() {
        $page ='
        <html>
        <head>
            <title>'.$this->meta_title.'</title>
            <meta name="keywords" content="'.$this->meta_keywords.'" />
        </head>
        <body>
            '.$this->html_body.'
        </body>
        </html>
';
        echo $page;
    }
}

, ..

$page = new Page();
$page->meta_title ="Hello world!";
$page->meta_keywords = "some text keywords";
$page->body = '<h1>Contact Us </h1>
<p>you can contact us at blah ... blah .. etc.</p>
<address>Dummy Address </address>
';

$page->displayPage();

, , .. ( ) , javascripts .. , .

, , . , $show_right_bar, $show_left_bar, , . , .

PHP-, , .

, .

+8

, , . PHP , . , . . , , XML . .

$page['html']['title'] = "Page Title";
$page['body']['p'][1] = "1st Paragraph";
$page['body']['p'][2] = "2nd Paragraph";

, HTML-.

0

MVC (Model, View, Controller), KohanaPHP. , . - . Kohana, , :

class Home_Controller extends Controller
{
  public function index()
  {
    echo "Hello World";
  }
}

, urur url, :

http://www.mysite.com/home/ (index() /, )

, , Model. , :

class Users_Model extends Model
{
  public function count_users()
  {
    return $this->db->count_records('users');
  }
}

, . Kohana Query Builder.

Controller, , . :

class Home_Controller extends Controller
{
  public function index()
  {
    $usersModel = new Users_Model;
    $userCount = $usersModel->count_users();

    echo "We have " . $userCount . " users!";
  }
}

, HTML/CSS/Javascript. " Views, . , echo print , ( , HTML-) :

class Home_Controller extends Controller
{
  public function index()
  {
    $myView = new View("index");
    $usersModel = new Users_Model;
    $userCount = $usersModel->count_users();

    $myView->userCount = $userCount;
    $myView->render(TRUE);
  }
}

""

<p>We have <?php print $userCount; ?> users!</p>

, . MVC- .

0

, .

, . "", index.php get get . , index.php. , /:

<?php

$page = strtolower($_GET['page']);
if(!isset($page) || $page === '')
{
    $page="home";
}

switch($page)
{
    case "loomis":
        require_once("loomis.php");
        break;
    case "home":
    default:
        $page = "home";
        require_once("home.php");
        break;
}

include "menu.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Nebulous - <?php getTitle($page) ?></TITLE>
<link href="style.css" rel="stylesheet" type="text/css" />
</HEAD>

<body>
<?php displayMenu() ?>
</body>
</html>

, .

0

OOP , - . (, , )

$index = new Page();
$index->displayHeader();
// php/html per needs
$index->displayFooter();

, , , , . .

0

- html. - HTML . , - html , . : HTML , .

html, -, . , , . , , , .., . webpage- > header() . , webpage- > heading ($ string, $size), , webpage- > paragraph ($ text), webpage- > image() .. webpage- > footer().

To make the webpage class worthwhile, also create css. For example, if you want to use bootstrap, you can include the css class and the style of your code. Note that you can also create a webpage class to create additional objects, such as webpage-> breadcrumbs () or webspage-> pagination ()

0
source

Source: https://habr.com/ru/post/1711856/


All Articles