美文网首页
开发企业网站 -- 项目搭建

开发企业网站 -- 项目搭建

作者: 潘肚饿兵哥哥 | 来源:发表于2019-09-25 22:25 被阅读0次

\color{rgba(254, 67, 101, .8)}{配置虚拟主机}

1. 打开Apache配置文件 httpd.conf中查找Include conf/extra/httpd-vhosts.conf

\color{rgba(254, 67, 101, .8)}{如果这一句是注释掉的,取消注释}

image.png

2. 根据上面的路径,在Apache文件夹中找到httpd-vhosts.conf这个文件

image.png

3.打开文件,将截图部分复制一份粘贴在此文档最下方,并删除掉注释符号,将DocumentRoot后面的名字改成项目路径:

此项目路径在c盘:C:/phpStudy/PHPTutorial/WWW/company

image.png

4. 将servername改成自己网站的域名:

image.png

\color{rgba(254, 67, 101, .8)}{最重要的就是DocumentRoot和ServerName这两条}
\color{rgba(254, 67, 101, .8)}{其他的个人项目可有可无,是用来配置权限的}

5.重启Apache

6.打开文件夹:C:\Windows\System32\drivers\etc中的hosts文件,加入这一句:127.0.0.1 www.yixing666.com

image.png

\color{rgba(254, 67, 101, .8)}{文件目录:}

一般会建文件夹分别装前后台文件
例如 home、index等文件夹就可能是装前台的文件
admin就是装后台的文件

image.png

\color{rgba(254, 67, 101, .8)}{通过网址打开首页:}

    /*
        index.php相当于一个入口
        这就是项目入口   

        m   moudel 模块
        c  controller 控制器

        www.yixing666.com?m=home&c=news  进入b模块(文件夹home)中的c控制器名称(具体哪一个页面)
    */


    //m是文件夹名,如果有(网址中能接收到这个值)就进入这个模块(文件夹),没有就进入前台模块(home文件夹)
    $m = isset($_GET['m']) ? $_GET['m'] : 'home'; 
    
    //c是文件名,如果有(网址中能接收到这个值)就进入这个控制器(文件),没有就给index(控制器名称,就是首页文件名index.php)
    $c = isset($_GET['c']) ? $_GET['c'] : 'index';

    $filename =  $m.'/'.$c.'.php';//构造出网址: home/news.php
    include_once($filename);//include 包含并运行指定文件,此时输入网址www.yixing666.com已经可以打开home文件夹里的index.php文件并显示出其中的内容了

现在只需要修改m、c的值,就可以进入建好的这些页面
image.png

\color{rgba(254, 67, 101, .8)}{如果用户输入的网址参数m、c有误的情况:}

<?php
    /*
        index.php相当于一个入口
        这就是项目入口   

        m   moudel 模块
        c  controller 控制器

        www.yixing666.com?m=home&c=news  进入b模块(文件夹home)中的c控制器名称(具体哪一个页面)
    */


    //m是文件夹名,如果有(网址中能接收到这个值)就进入这个模块(文件夹),没有就进入前台模块(home文件夹)
    $m = isset($_GET['m']) ? $_GET['m'] : 'home'; 
    

    //c是文件名,如果有(网址中能接收到这个值)就进入这个控制器(文件)
    //没有就给index(控制器名称,就是首页文件名index.php)
    $c = isset($_GET['c']) ? $_GET['c'] : 'index';


    $filename =  $m.'/'.$c.'.php';//构造出网址: home/news.php


    //file_exists()函数检查文件或目录是否存在。
    //假如用户输错m或c的话:
    //进行判断,如果输入正确,就显示正确页面,输入不正确,就输出页面不存在
    if(file_exists($filename)){
        include_once($filename);
    }else{
        die("($filename)不存在");
    }

    //include_once($filename);include 包含并运行指定文件,此时输入网址www.yixing666.com已经可以打开home文件夹里的index.php文件并显示出其中的内容了
?>

\color{rgba(254, 67, 101, .8)}{检测用户是否通过入口访问页面}

在入口文件(首页)用这个函数创建一个常量作为key,在其他页面接收检测这个常量
define('TICKET', 1);

检测 是否通过入口访问:
defined('TICKET') or die('Denny');

\color{rgba(254, 67, 101, .8)}{由于文件多,检测代码又是每个文件必须,所以写在公共文件中}
\color{rgba(254, 67, 101, .8)}{通过公共文件引入的页面}
\color{rgba(254, 67, 101, .8)}{这样做也方便后期可以统一修改}
\color{rgba(254, 67, 101, .8)}{方便做统一的内容处理,例如拦截等}
\color{rgba(254, 67, 101, .8)}{单一入口不代表是唯一入口}


\color{rgba(254, 67, 101, .8)}{项目基础框架:}

image.png
index.php相当于一个入口
        这就是项目入口   


        www.yixing666.com?m=home&c=news  进入b模块(文件夹home)中的c控制器名称(具体哪一个页面)

        通过建立:
            入口文件建立凭证 => 公共文件验证凭证 => 其他页面引入公共文件
        的架子实现验证,这就是单入口模式

        单入口模式特点:
            安全(不用这样入口的话,在浏览器中随便打一个文件夹可能就可以访问了)

\color{rgba(254, 67, 101, .8)}{入口文件:index.php}

<?php
    /*
        index.php相当于一个入口
        这就是项目入口   

        m   moudel 模块
        c  controller 控制器

        www.yixing666.com?m=home&c=news  进入b模块(文件夹home)中的c控制器名称(具体哪一个页面)

        通过建立:
            入口文件建立凭证 => 公共文件验证凭证 => 其他页面引入公共文件
        的架子实现验证,这就是单入口模式

        单入口模式特点:
            安全(不用这样入口的话,在浏览器中随便打一个文件夹可能就可以访问了)
    */


    //m是文件夹名,如果有(网址中能接收到这个值)就进入这个模块(文件夹),没有就进入前台模块(home文件夹)
    $m = isset($_GET['m']) ? $_GET['m'] : 'home'; 
    

    //c是文件名,如果有(网址中能接收到这个值)就进入这个控制器(文件)
    //没有就给index(控制器名称,就是首页文件名index.php)
    $c = isset($_GET['c']) ? $_GET['c'] : 'index';


    //要产生一个凭证用户通过这个凭证可以去往其他页面
//然后其他页面通过检测这个凭证确认是否让用户访问
    //产生一个凭证  使用常量(定义后不能被更改)
    //常量前面不能有 $ 符号
    //通常全部大写(用于区分)
    //define()  定义一个常量。
    //define('name', value);
    //其他所有页面都要加检测语句 defined('TICKET') or die('Denny');
    define('TICKET', 1);
    

    $filename =  $m.'/'.$c.'.php';//构造出网址: home/news.php


    //file_exists()函数检查文件或目录是否存在。
    //假如用户输错m或c的话:
    //进行判断,如果输入正确,就显示正确页面,输入不正确,跳转到404页面
    if(file_exists($filename)){
        include_once($filename);
    }else{
        header("location: 404.html");
    }

    //include_once($filename);include 包含并运行指定文件,此时输入网址www.yixing666.com
//已经可以打开home文件夹里的index.php文件并显示出其中的内容了
?>

\color{rgba(254, 67, 101, .8)}{404文件}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>404</title>
</head>
<body>
    <h1 align="center">404</h1>
    <p align="center">页面不存在</p>
</body>
</html>

\color{rgba(254, 67, 101, .8)}{前端文件:index.php}


<?php
    require_once('init.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>前台首页</h1>
</body>
</html>

\color{rgba(254, 67, 101, .8)}{前端文件:news.php}


<?php
    require_once('init.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>新闻列表</h1>
</body>
</html>

\color{rgba(254, 67, 101, .8)}{前端配置文件:init.php}

用于验证入口文件传过来的参数

<?php
/*
    init.php 初始化文件 公共文件
*/


    //检测是否通过入口访问
    //define 创建一个常量  defined 检测常量 ,就一个 d 的差别
    //这行代码作用是 检测到这个常量就继续,否则退出
    //退出可以是输出一行提示消息,也可以跳转到某个页面
    defined('TICKET') or die('Denny');
?>

\color{rgba(254, 67, 101, .8)}{后端文件:index.php}


<?php
    require_once('init.php');
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>后台首页</h1>
</body>
</html>

\color{rgba(254, 67, 101, .8)}{后端文件:news.php}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>编辑新闻</h1>
</body>
</html>

\color{rgba(254, 67, 101, .8)}{后端文件:init.php}

用于验证入口文件传过来的参数

<?php
    defined('TICKET') or die('Denny');
?>

相关文章

网友评论

      本文标题:开发企业网站 -- 项目搭建

      本文链接:https://www.haomeiwen.com/subject/plkgyctx.html