Php 使用codeigniter创建主页
如何使用codeigniter创建主页 该页面应该包含一些链接,如登录、注册等 我按照图坦卡蒙的指示创建了一个登录屏幕。但它使codeigniter仅用于此目的。这就是我所说的网站: 所以基本上我想做的是,使用codeigniter做更多的事情,而不仅仅是一个登录表单 My try routes.php我设置了以下设置:Php 使用codeigniter创建主页,php,codeigniter,Php,Codeigniter,如何使用codeigniter创建主页 该页面应该包含一些链接,如登录、注册等 我按照图坦卡蒙的指示创建了一个登录屏幕。但它使codeigniter仅用于此目的。这就是我所说的网站: 所以基本上我想做的是,使用codeigniter做更多的事情,而不仅仅是一个登录表单 My try routes.php我设置了以下设置: $route['default_controller'] = "mainpage"; $route['login'] = "login"; My mainpage.php文
$route['default_controller'] = "mainpage";
$route['login'] = "login";
My mainpage.php文件:
class Mainpage extends Controller
{
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.html');
}
}
Mainpage.html:
<HTML>
<HEAD>
<TITLE></TITLE>
<style>
a.1{text-decoration:none}
a.2{text-decoration:underline}
</style>
</HEAD>
<BODY>
<a class="2" href="login.php">login</a>
</BODY>
</HTML>
a、 1{文本装饰:无}
a、 2{文本装饰:下划线}
Login.php与我在本文中为其提供链接的网站中的Login.php完全相同:
Class Login extends Controller
{
function Login()
{
parent::Controller();
}
function Index()
{
$this->load->view('login_form');
}
function verify()
{
if($this->input->post('username'))
{ //checks whether the form has been submited
$this->load->library('form_validation');//Loads the form_validation library class
$rules = array(
array('field'=>'username','label'=>'username','rules'=>'required'),
array('field'=>'password','label'=>'password','rules'=>'required')
);//validation rules
$this->form_validation->set_rules($rules);//Setting the validation rules inside the validation function
if($this->form_validation->run() == FALSE)
{ //Checks whether the form is properly sent
$this->load->view('login_form'); //If validation fails load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> form again
}
else
{
$result = $this->common->login($this->input->post('username'),$this->input->post('password')); //If validation success then call the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> function inside the common model and pass the arguments
if($result)
{ //if <b style="color: black; background-color: rgb(153, 255, 153);">login</b> success
foreach($result as $row)
{
$this->session->set_userdata(array('logged_in'=>true,'id'=>$row->id,'username'=>$row->username)); //set the data into the session
}
$this->load->view('success'); //Load the success page
}
else
{ // If validation fails.
$data = array();
$data['error'] = 'Incorrect Username/Password'; //<b style="color: black; background-color: rgb(160, 255, 255);">create</b> the error string
$this->load->view('login_form', $data); //Load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> page and pass the error message
}
}
}
else
{
$this->load->view('login_form');
}
}
}
类登录扩展控制器
{
函数登录()
{
父::控制器();
}
函数索引()
{
$this->load->view('login_form');
}
函数验证()
{
如果($this->input->post('username'))
{//检查表单是否已提交
$this->load->library('form_validation');//加载form_validation library类
$rules=数组(
数组('field'=>'username','label'=>'username','rules'=>'required'),
数组('field'=>'password','label'=>'password','rules'=>'required')
);//验证规则
$this->form_validation->set_rules($rules);//在验证函数中设置验证规则
如果($this->form\u validation->run()==FALSE)
{//检查表单是否正确发送
$this->load->view('login_form');//如果验证失败,请再次加载登录表单
}
其他的
{
$result=$this->common->login($this->input->post('username'),$this->input->post('password'));//如果验证成功,则调用公共模型内的login函数并传递参数
如果($结果)
{//如果登录成功
foreach($结果为$行)
{
$this->session->set_userdata(数组('logged_in'=>true,'id'=>$row->id,'username'=>$row->username));//将数据设置到会话中
}
$this->load->view('success');//加载成功页面
}
其他的
{//如果验证失败。
$data=array();
$data['error']=“用户名/密码不正确”;//创建错误字符串
$this->load->view('login_form',$data);//加载登录页面并传递错误消息
}
}
}
其他的
{
$this->load->view('login_form');
}
}
}
我遗漏了什么?请注意,您没有为主控制器指定正确的方法名称:
class Mainpage extends Controller
{
function Welcome() // < problem should be Mainpage
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.html');
}
}
并使用此文件名修改主控制器,例如:
class Mainpage extends Controller
{
function Mainpage()
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.php');
}
}
你用的是CodeIgniter,对吗?您的配置中是否有一些规定,必须在所有URL上使用.php作为扩展?如果没有,您应该将HREF发送到“/login”not“/login.php”。此外,如果您还没有从htaccess文件和CI配置中的URL中删除“index.php”,那么您可能需要将其包含在链接中 此外,如果我是你,我不会遵循Sarfraz在Mainpage.php文件中的做法。您不应该在CodeIgniter中使用标准PHP includes。使用include完成的任何操作都可以通过加载视图轻松完成。例如,如果要将视图作为字符串加载,可以说:
$loginViewString = $this->load->view('login.php', '', true);
其中,第二个参数是在关联数组中传递给视图的任何信息,其中键是要传递的变量的名称,值是值。那就是
$dataToPassToView = array('test'=>'value');
$loginViewString = $this->load->view('login.php', $dataToPassToView, true);
然后在login.php视图中,您可以引用变量$test,该变量的值为“value”
此外,您实际上不需要声明“登录”路由,因为您只是将其重定向到“登录”控制器。您可以使用“用户”控制器和“登录”方法,并按如下方式声明您的路线:
$routes['login'] = 'user/login';
编辑…
好吧,我想这可能是在错误的方向上偏离了一两步。让我们重新开始,好吗
首先,让我们从与本次讨论相关的文件列表开始:
class Main extends Controller
{
function __construct() //this could also be called function Main(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function index()
{
$this->load->view('header.php');
$this->load->view('splashpage.php');
$this->load->view('footer.php');
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="This is the text people will read about my website when they see it listed in search engine results" />
<title>Yustme's Site</title>
<!-- put your CSS includes here -->
</head>
<body>
</body>
<!-- put your javascript includes here -->
</html>
class User extends Controller
{
function __construct() //this could also be called function User(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function login()
{
$this->load->view('header.php');
$this->load->view('login.php');
$this->load->view('footer.php');
}
}
<div id="login_box">
<!-- Put your login form here -->
</div>
//add this line to the end of your routes list
$routes['login'] = '/user/login';
现在让我们看一下页眉、页脚和SpopHelpPoad视图:
application/views/header.phpclass Main extends Controller
{
function __construct() //this could also be called function Main(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function index()
{
$this->load->view('header.php');
$this->load->view('splashpage.php');
$this->load->view('footer.php');
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="This is the text people will read about my website when they see it listed in search engine results" />
<title>Yustme's Site</title>
<!-- put your CSS includes here -->
</head>
<body>
</body>
<!-- put your javascript includes here -->
</html>
class User extends Controller
{
function __construct() //this could also be called function User(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function login()
{
$this->load->view('header.php');
$this->load->view('login.php');
$this->load->view('footer.php');
}
}
<div id="login_box">
<!-- Put your login form here -->
</div>
//add this line to the end of your routes list
$routes['login'] = '/user/login';
application/views/login.php
class Main extends Controller
{
function __construct() //this could also be called function Main(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function index()
{
$this->load->view('header.php');
$this->load->view('splashpage.php');
$this->load->view('footer.php');
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="This is the text people will read about my website when they see it listed in search engine results" />
<title>Yustme's Site</title>
<!-- put your CSS includes here -->
</head>
<body>
</body>
<!-- put your javascript includes here -->
</html>
class User extends Controller
{
function __construct() //this could also be called function User(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function login()
{
$this->load->view('header.php');
$this->load->view('login.php');
$this->load->view('footer.php');
}
}
<div id="login_box">
<!-- Put your login form here -->
</div>
//add this line to the end of your routes list
$routes['login'] = '/user/login';
就这样。没有魔法什么的。我之所以提出可以将视图作为字符串加载,是因为您可能不希望有单独的“页眉”和“页脚”视图。在这种情况下,您可以简单地将一个视图作为字符串“回显”到另一个视图中。另一个例子是如果你有一个sho