如何使用codeigniter创建主页?
该页面应包含一些链接,如登录,注册等.
我跟着啧啧创建了一个登录界面.但它只为此目的制作了codeigniter.这是我正在谈论的网站:
http://tutsmore.com/programming/php/10-minutes-with-codeigniter-creating-login-form/
所以基本上我尝试的是,使用codeigniter来做更多的事情,而不仅仅是一个登录表单.
我尝试routes.php我设置这些设置:
$route['default_controller'] = "mainpage";
$route['login'] = "login";
Run Code Online (Sandbox Code Playgroud)
我的mainpage.php文件:
class Mainpage extends Controller
{
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.html');
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想念的是什么人?
你正在使用CodeIgniter,对吧?您的配置中是否有一些规定,您必须使用.php作为所有URL的扩展名?如果没有,您应该将您的hrefs发送到"/ login"而不是"/login.php".此外,如果您尚未从htaccess文件和CI配置中的URL中删除"index.php",则可能需要将其包含在链接中.
此外,如果我是你,我不会遵循Sarfraz在他的Mainpage.php文件中所做的事情.您不应该在CodeIgniter中使用标准PHP包含.使用include完成的任何操作都可以通过加载视图轻松完成.例如,如果要将视图作为字符串加载,可以说:
$loginViewString = $this->load->view('login.php', '', true);
Run Code Online (Sandbox Code Playgroud)
其中第二个参数是您想要在关联数组中传递给视图的任何信息,其中键是您将传递的变量的名称,值是值.那是...
$dataToPassToView = array('test'=>'value');
$loginViewString = $this->load->view('login.php', $dataToPassToView, true);
Run Code Online (Sandbox Code Playgroud)
然后在login.php视图中,您可以引用变量$ test,其值为"value".
此外,您实际上不需要声明"登录"路由,因为您只是将其重定向到"登录"控制器.你可以做的是有一个"用户"控制器与"登录"方法,并声明你的路线如下:
$routes['login'] = 'user/login';
Run Code Online (Sandbox Code Playgroud)
编辑...
好吧,我认为这可能在错误的方向上偏离了一两步.让我们重新开始,好吗?
首先让我们从与此讨论相关的文件列表开始:
所以,现在让我们看看每个文件中的代码,它们将实现你想要做的事情.首先是main.php控制器(再次,它将是您的默认控制器).当您访问网站的根地址时,将调用此... www.example.com
应用/控制器/ main.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');
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们来看看页眉,页脚和启动页面视图:
应用/视图/ header.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>
Run Code Online (Sandbox Code Playgroud)
application/views/splashpage.php - 注意:你没有理由在这里需要一个包装器div ...它只是作为一个例子放在那里
<div id="splashpagewrapper">
<a href="/login">Click here to log in</a>
</div>
Run Code Online (Sandbox Code Playgroud)
应用/视图/ footer.php
</body>
<!-- put your javascript includes here -->
</html>
Run Code Online (Sandbox Code Playgroud)
现在让我们看一下User控制器和login.php视图:
应用/控制器/ user.php的
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');
}
}
Run Code Online (Sandbox Code Playgroud)
应用/视图/ login.php中
<div id="login_box">
<!-- Put your login form here -->
</div>
Run Code Online (Sandbox Code Playgroud)
最后,make/login的路由查找/ user/login:
应用/配置/ routes.php文件
//add this line to the end of your routes list
$routes['login'] = '/user/login';
Run Code Online (Sandbox Code Playgroud)
就是这样.没有魔法或任何东西.我提出可以将视图加载为字符串这一事实的原因是因为您可能不希望具有单独的"标题"和"页脚"视图.在这种情况下,您可以简单地将视图"回显"为字符串INTO另一个视图.另一个例子是,如果您有一个装满商品的购物车,并且您希望购物车和商品是单独的视图.您可以遍历您的项目,将"shoppingcartitem"视图作为每个项目的字符串加载,将它们连接在一起,并将该字符串回显到"shoppingcart"视图中.
那应该是它.如果您还有疑问,请告诉我.