可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Basically I have two controllers using CodeIgniter for a simple blog for a project at school. One is Home which is the login page. The other is Member for when they are signed in. in this member controller I am creating functions for add_post(), view_users_posts(), etc. Member.php
class Member extends CI_Controller{ function __construct(){ parent::__construct(); $is_logged_in = $this->session->userdata('is_logged_in'); if (!isset($is_logged_in) || $is_logged_in !== true) { redirect('home'); } } public function index() { $this->load->library('pagination'); $config['base_url'] = base_url() . 'index.php/member/index/'; $config['total_rows'] = $this->db->get('posts')->num_rows(); $config['per_page'] = 5; $config['num_links'] = 20; $this->pagination->initialize($config); $data['posts'] = $this->posts_model->get_all_posts($config['per_page'], $this->uri->segment(3)); $this->load->view('member_view', $data); } public function add_post() { $this->load->view('add_post_view'); }
My member view has a nav menu with links to these functions like so: member_view.php
<li><a href="member">Home</a></li> <li><a href="member/add_post">Add Post</a></li> <li><a href="member/view_users_posts">My Posts</a></li> <li><a href="member/reset_pass">Reset Password</a></li> <li><a href="member/logout">Logout</a></li>
I haven't finished them as I am having trouble knowing what to code. Basically if the user clicks Add Post should it refer to the function add_post in the controller which will load a view called add_post? Or should it direct to a new controller called Add_post that will load the view?
My reason is if I click add post it takes me to member/add_post but when I click back home I get member/member then add post again I get member/member/add_post.
I hope this makes sense, I am at a total loss. Thanks for any help!
回答1:
you need to use base url
.
Suppsoe your base url is (in the config), www.example.com/
then your code will be
<li><a href="<?php echo base_url('member/add_post');?>">Add Post</a></li>
Do the same for other items.
Note: Remember slash in the config file. otherwise include it in base_url('/member/add_post')
Reference
回答2:
You are on the right track. For the menu items you have, you would have to complete these controller functions:
class Member extends CI_Controller { function __construct() { parent::__construct(); $is_logged_in = $this->session->userdata('is_logged_in'); if (!isset($is_logged_in) || $is_logged_in !== true) { redirect('home'); } function add_post() { // for example: $this->load->view('header'); // standard header and menu view $this->load->view('add_post'); // input form view $this->load->view('footer'); // standard footer, adverts, etc. } function view_users_posts() { $data ['posts'] = $this->model->get_all_posts( ...(etc)... ); $this->load->view('header'); // standard header and menu view $this->load->view('view_users_posts', $data); $this->load->view('footer'); // standard footer, adverts, etc. } function reset_pass() { ... } function logout() { ... } }
The first new function references views/header.php
, views/add_post.php
, and views/footer.php
. The other "member" functions should probably also load header and footer (for a consistent site look) but load a different main body of functionality, probably called something very similar to the functions.
I have assumed view_users_posts()
accesses some new model function to retrieve the posts, passes it along to the view to format appropriately. Note that view/view_users_posts.php
should access the variable $posts
based on the way I wrote it above.
回答3:
Some time ago i had the same problem. My Solution was that is set the base url to the path to the index.php. For example
$config['base_url'] = "http://localhost/codeigniter/index.php/"
so you can use it like this
<a href="<?php echo base_url() ?>Controller/Action/Param1/Param2">Link Text</a>