当我尝试添加一个新的函数到我现有的产品时,我得到了以下错误。我是php codeIgniter的新手,已经在这方面卡住了一段时间。任何帮助将不胜感激,并提前感谢。如果需要任何进一步的文件,让我知道,我会更新帖子。当我试图提交search_add_view表单时,我得到了以下错误:
Error Was Encountered The action you have requested is not allowed.
我的配置文件控制器profile.php是:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Profile extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('download');
$this->load->library('form_validation');
$this->load->library('table');
$this->load->model('searches_model');
$this->searches = $this->searches_model->get_searches();
$this->sites = $this->searches_model->get_sites();
$this->load->driver('scraper', array_column($this->sites, 'driver'));
//check auth
if (!is_admin() && !is_user()) {
redirect(base_url());
}
}
public function index()
{
if (user()->qr_code == '') {
$this->generate_qucode(user()->slug);
}
$data = array();
$data['page_title'] = 'Profile';
$data['user'] = $this->admin_model->get_user_info();
$data['payment'] = $this->admin_model->get_my_payment();
$data['features'] = $this->admin_model->active_features($data['payment']->package_id);
$data['fonts'] = $this->admin_model->select('google_fonts');
$data['countries'] = $this->admin_model->select('country');
$data['main_content'] = $this->load->view('admin/user/profile', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function search_index()
{
$this->form_validation->set_rules('search[name]', 'Search Name', 'required');
$this->form_validation->set_rules('search[site_id]', 'Site', 'required');
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$data = $this->input->post('search', TRUE);
$search_id = $this->searches_model->add_search($data);
redirect();
}
$data = array(
'subview' => 'search_add_view',
'sites' => $this->sites,
'searches' => $this->searches
);
$data['page_title'] = 'Search View';
$data['subview'] = $this->load->view('admin/scraper/search_add_view', $data, TRUE);
$data['sites'] = $this->sites;
$data['searches'] = $this->searches;
$data['main_content'] = $this->load->view('admin/scraper/searches_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function edit($search_id)
{
$search = $this->searches_model->get_search($search_id);
$this->form_validation->set_rules('search[name]', 'Search Name', 'required');
$this->form_validation->set_rules('search[site_id]', 'Site', 'required');
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$data = $this->input->post('search', TRUE);
$this->searches_model->update_search($search_id, $data);
redirect();
}
$data = array(
'subview' => 'search_edit_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_edit_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
/**
* Delete a search
*
* @param integer search id to delete
* @return none
*/
public function delete($search_id)
{
$search = $this->searches_model->get_search($search_id);
$search['id'] = $search_id;
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
$data = array('id' => $search_id);
$this->searches_model->delete_search($data);
redirect();
}
$data = array(
'subview' => 'search_delete_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_delete_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
public function execute($search_id)
{
$search = $this->searches_model->get_search($search_id);
$this->form_validation->set_rules('search[url]', 'Search URL', 'required');
if ($this->form_validation->run())
{
$search['url'] = $this->input->post('search[url]', true);
$output = $this->scraper->scrape($search['url'], $search['driver']);
force_download($search['name'] . '.csv', $output);
}
$data = array(
'subview' => 'search_execute_view',
'sites' => $this->sites,
'searches' => $this->searches,
'search' => $search
);
$data['main_content'] = $this->load->view('admin/scraper/search_execute_view', $data, TRUE);
$this->load->view('admin/index', $data);
}
}
当前位置:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'home/index';
$route['404_override'] = 'home/error_404';
$route['translate_uri_dashes'] = FALSE;
$route['index'] = 'home/index';
$route['error-404'] = 'home/error_404';
$uri = $this->uri->segment(1);
if ( $uri == 'searches_view' || $uri == 'search_add_view' || $uri == 'search_execute_view' || $uri == 'search_delete_view' || $uri == 'search_edit_view') {
$route['search_add_view/(:any)'] = 'admin/profile/search_add_view/$1';
$route['searches_view'] = 'admin/profile/search_index';
$route['search_execute_view'] = 'admin/profile/search_execute_view';
$route['search_delete_view'] = 'admin/profile/search_delete_view';
$route['search_edit_view'] = 'admin/profile/search_edit_view';
}
当前位置:
<?php include'include/header.php';?>
<?php include'include/left_sideber.php';?>
<div style="height:100vh; margin-left: 230px;" class="content">
<?php echo $subview;?>
<h2>Saved Searches</h2>
<?php
if (count($searches) == 0)
{
echo '<p>There are no saved searches.</p>';
}
else
{
$this->table->set_heading('Search Name', 'Site', 'Search URL', 'Edit', 'Delete', 'Execute');
foreach ($searches as $search)
{
$this->table->add_row(
$search['name'], $search['site_name'], $search['url'],
anchor('searches/edit/' . $search['id'], 'Edit'),
anchor('searches/delete/' . $search['id'], 'Delete'),
anchor('searches/execute/' . $search['id'], 'Execute')
);
}
echo $this->table->generate();
}
?>
<?php echo $main_content;?>
<?php include'include/footer.php';?>
php文件夹:
<div class="content">
<form method="post">
<fieldset>
<legend>Add a New Search</legend>
<p class="space">
<label for="name">Search Name</label>
<input type="text" id="name" name="search[name]" value="<?php echo set_value('search[name]', ''); ?>" size="32" maxlength="32" autofocus>
<?php echo form_error('search[name]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p>
<label for="sites">Site</label>
<select id="sites" name="search[site_id]">
<option value="" selected>Select one</option>
<?php foreach ($sites as $row):?>
<option value="<?=$row['id']?>" <?php echo set_select('search[site_id]', $row['id']); ?>><?=$row['name']?></option>
<?php endforeach;?>
</select>
<?php echo form_error('search[site_id]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p>
<label for="url">Search URL</label>
<input type="text" id="url" name="search[url]" value="<?php echo set_value('search[url]', ''); ?>" size="105" maxlength="254">
<?php echo form_error('search[url]', ' <br>', '<br>' . PHP_EOL); ?>
</p>
<p><input type="submit" name="submit" value="Ok"></p>
</fieldset>
</form>
<p> </p>
</div>
search_model.php文件类型:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Searches_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_searches()
{
return $this->db->query(
'SELECT
searches.id AS id,
searches.name AS name,
searches.url AS url,
sites.id AS site_id,
sites.name AS site_name,
sites.driver AS driver
FROM searches
INNER JOIN sites ON
searches.site_id = sites.id
;'
)->result_array();
}
public function get_search($search_id)
{
$query = $this->db->query(
'SELECT
searches.name AS name,
searches.url AS url,
sites.id AS site_id,
sites.name AS site_name,
sites.driver AS driver
FROM searches
INNER JOIN sites ON
searches.site_id = sites.id
WHERE searches.id = ' . $search_id . '
;'
)->result_array();
return $query[0];
}
public function add_search($data)
{
$this->db->insert('searches', $data);
return $this->db->insert_id();
}
public function update_search($id, $data)
{
$this->db->update('searches', $data, array('id' => $id));
}
public function delete_search($data)
{
$this->db->delete('searches', $data);
}
public function get_sites()
{
return $this->db->query(
'SELECT
sites.id AS id,
sites.name AS name,
sites.driver AS driver
FROM sites
;'
)->result_array();
}
}
我文件结构是:第一次
1条答案
按热度按时间csbfibhn1#
我设法通过两个步骤解决了这个问题:首先,我确认config.json包含以下属性:
第二,我必须在表单声明之后将以下代码添加到html: