CodeIgniter parameters - unable to load requested file - php

I have this controller:
class Work extends MY_Controller {
public function assign($id) {
//validation checks
if ($this->form_validation->run() === FALSE) {
$this->load->view("work/assign/" . $id);
} else {
//success
}
}
}
As well as a view at views/work/assign.php I also have the following route:
$route['work/assign/(:any)'] = 'work/assign/$1';
When I go to /work/assign/1 in my browser, and I get this error: Unable to load the requested file: work/assign/1.php I want it to render the page with the parameter 1

The loader doesn't care about your routes, and besides:
$route['work/assign/(:any)'] = 'work/assign/$1';
...doesn't do anything. This code:
$this->load->view("work/assign/" . $id);
...will always look for a file in APPPATH/views/work/assign/$id.php which I assume does not exist.
You want to use the second paramter in view() to load variables, for example:
$this->load->view("work/assign", array('id' => $id));
This will load the view file work/assign.php and import the variable $id.

The error you are getting is due to the wrong parameter in the view call
$this->load->view("work/assign/" . $id); since your last trailing slash tell CI that this is the .php and in your last trailing slash your $id=1 so it searches to load the 1.php the correct way to load the view and pass your id to view is make an array which contains the data you want to show in view then call view and pass the array, the array keys will converted to variables by CI
class Work extends MY_Controller {
public function assign($id) {
//validation checks
if ($this->form_validation->run() === FALSE) {
$data['id']=$id;
$this->load->view("work/assign" ,$data);
} else {
//success
}
}
}
In assign.php just echo $id and you will get desired id

Related

Error 404 despite that page exists Codeigniter

I have something like website.com/profile/nameofuser that is working.
But if I have website.com/profile/_nameofuser I get 404 error also website.com/profile/nameofuser_ or website.com/profile/nameof_user is working. It's not something related to accepted characters, but what's the problem?
class Profile extends CI_Controller {
public function __construct()
{
parent:: __construct();
$this->load->model('Profile_model');
$this->load->helper(array('url', 'form', 'htmlpurifier'));
}
public function index() {
$this->load->library('form_validation');
if(getUserData($this->uri->segment(2), "ID") < 0) {
$this->session->set_flashdata('error', 'Profil inexistent.');
redirect(base_url());
}
if (!is_cache_valid(md5('profile' . $this->uri->segment(2) . ''), 300)){
$this->db->cache_delete('profile', $this->uri->segment(2));
}
if(getUserData($this->uri->segment(2), "ID") > 0) {
/* some mysql queries.. */
}
$data["main_content"] = 'profile/profile_view';
$this->load->view('includes/template.php', $data);
} else {
$this->session->set_flashdata('error', 'Profil inexistent.');
redirect(base_url());
}
}
function _remap($method,$args)
{
if (method_exists($this, $method))
{
$this->$method($args);
}
else
{
$this->index($method,$args);
}
}
}
Here is my profile controller. I really don't know what's the problem. If I enter an invalid profile redirects with error flashdata so ti's ok. Maybe it's a remap problem?
1) Check the fie format in your view file.. if it is html file that mean you cannot call it without its format
for example
if your file is php format have name home-view.php
you can call it as
$this->load->view('home-view');
but if it is html file then the name in home-view.html
so you have to call it with its exstention as
$this->load->view('home-view.html');
your call to /profile/nameofuser is missing a fundamental from the MVC architecture.
You need to call a controller/method combination (in CI, your basic url is domain.com/controller/method)...
since you don't have a specific method for each user within the profile controller, which is actually a good thing, you need a method within the controller that handles your users. You already have it, it's called index
If you point your URL to /profile/index/nameofuser and change $this->uri->segment(2) to $this->uri->segment(3) you should get it to work
give it a try and let me know

How to use redirect in codeigniter controller class?

Can any one tel me how to use redirect in controller class.
I am wrote below code:
Controller:-
<?php
class Login extends CI_Controller {
public function result()
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$this->index();
redirect('/success', 'location');
}
}
view:-
success.php
<?php
echo "Success page";
?>
It shows error message 404 Page Not Found.
I have load all required helper classes in autoload class.
All you need to do to redirect is
Example:
public function index() {
redirect('controller_name');
// you may need to set controller name in routes do not need location
redirect('folder/controller_name');
// you may need to set controller name in routes do not need location
}
In Codeigniter , redirect method takes 3 parameters.
redirect('/controller_name/method_name', 'location', 301);
First parameter is the uri path which you want to redirect. The second parameter is optional and takes "location" method (default) or the "refresh" method. The third optional parameter is status code. You can check detail on documentation.
Edit
function success () {
$data["message"] = "Success";
$this->load->view("success", $data);
}
views/success.php
<?php echo $message; ?>
You have to pass data in array because codeigniter use extract method to pass value in view so that you can use arrary key as variable.
Hope it will be useful for you.
You might be having these three files
routes.php where you can set your routes as like
$route['success'] = 'your_controller_name/your_method_name';
E.g.
$route['success'] = 'my_controller/success';
then within your_controller.php, there you have a method as
function success() {
$data['msg'] = "Success";
$this->load->view('success',$data);
}
and within your success.php
<?php echo "<h3>".$msg."<h3>";?>

Codeigniter routes for passing get parameter to index function

I have a url www.mywebsite.com/store/123456
where store is my controller class and I have a index function in it where Im getting the value after after store ,ie 123456.But im not able to achieve it.
As found online,I have tried adding this to routes $route['store/(:any)'] = 'store/index'; also tried
$route['store/(:any)'] = 'store/index/$1';
but doesn't seem to work.Please help me out.
In controller index function is
public function index()
{
echo $this->uri->segment(1);
}
But its not working.Pleae help
You are invoking 123456() method instead of index() method therefore you get CI's 404.
The simplest way is to use this kind of route
$route['store/(:any)'] = 'store/index/$1';
AND in top of it add parameter to index function in your case
public function index($parameter)
{
echo $this->uri->segment(2);
}
note that I changed segment parameter please see documentation.
using _remap()
function _remap($parameter){
$this->index($parameter);
}
function index($p)
{
echo $p; //shows 123456
}
If I remember correctly:
segment(n) gives you the segments of your uri before the routing takes place
rsegment(n) gives you the segments of your uri after routing (if routing occurred or the same as segment if it didn't)
so for /store/123456 getting rerouted to /store/index/123456
segment(1) == rsegment(1) == 'store'
rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'
Route:
$route['terms_and_conditions'] = 'SO_front/page/1';
controller :
public function page($id)
{
echo $id;
}
Output :
1
Here my solution for CI3...i would prefer laravel for advanced routing, orm,restapi, build in vuejs.
$route['store/(:any)'] = 'store/index/';
public function your index(){
$parameter=$this->uri->segment(2);
//use parameter for sql query.
}
Let say your route $route[store/product/update/(:any)] , then $parameter=$this->uri->segment(4)
Problem?. You will need to change entire file code if you plan to change the route name include view, controller, and custom route.

CI routing issue

i am currently learning CI and i have come to an issue that i cant seem to solve.
i set up my wamp server in a drive and inside the www(root) folder i have extracted the codeigniter files.
![example][1][1]: http://i.stack.imgur.com/7RKqG.png
then I created my php files for view/model and controller and set the default route in the config/routes.php
so now when I go to my browser and type localhost I get the post.php displayed without anyissue.
but I am unable to access any of the views from here. for example i have a new_post.php view and when i type in the address bar localhost/new_post.php i get a "Not Found
The requested URL /new_post.php was not found on this server." error.
what am i doing wrong? below i have posted the code which i have written in the post.php controller along with a image of the file structure/names i have.
posts.php - controller
<?php
class Posts extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('post'); //loads the post model u created in the models folder
}
function index() //goes to this function 1st when u access the controller
{
$data['posts']=$this->post->get_posts(); // load all the data from the get_posts function in post model to the data array posts
$this->load->view('post_index', $data); //loads the view
}
function post($postID)
{
$data['post']=$this->post->get_post($postID);
$this->load->view('post', $data);
}
function new_post()
{
if($_POST)
{
$data=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' =>1
);
$this->post->insert_post($data);
redirect(base_url(). 'posts/');
}
else
{
$this->load->view('new_post');
}
}
function editpost($postID)
{
$data['success']=0;
if($_POST)
{
$data_post=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' => 1
);
$this->post->update_post($postID,$data);
$data['success'] =1;
}
$data['post']=$this->post->get_post($postID);
$this->load->view('edit_post',$data);
}
function deletepost($postID)
{
$this->post->delete_post($postID);
redirect(base_url(). 'posts/');
}
}
![structure][1] [1]: http://i.stack.imgur.com/SnsbW.png
In CodeIgniter you have to use controller to get access to his function
you are saying "when i type in the address bar localhost/new_post.php i get a Not Found" because you try to direct access its function name you have to use example.com/controllername/functionname like this
http://localhost/posts/new_post
for more information check codeignier url
https://ellislab.com/codeigniter/user-guide/general/urls.html
if you not removed index.php using .htaccess then you have to use your url like this
http://localhost/index.php/posts/new_post
In CodeIgniter, everything runs through the main "index.php" file, in your root directory.
So, you would access your new post page, like this;
http://localhost/index.php/posts/new_post
Have a read through the CodeIgniter user guide, it will answer any problem you have.
https://ellislab.com/codeigniter/user-guide/

SEO url gives 404-error in CodeIgniter

I am pretty new to codeigniter. I do know php.
How can I accomplish to load the right view?
My url: /blog/this-is-my-title
I’ve told the controller something like
if end($this->uri->segment_array()) does exist in DB then load this data into some view.
I am getting an 404-error everytime I access /blog/whatever
What am i seeing wrong?
unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.
A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title
A route like:
$route['blog/(:any)'] = "blog/view/$1";
may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'
The may be more possibilities:
The most common - mod_rewrite is not active
.htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
The controller does not exist or is placed in the wrong folder
Suggestion: if you only need to change data use another view in the same controller
if (something)
{
$this->load->view('whatever');
}
else
{
$this->load->view('somethingelse');
}
If none of those works post a sample of code and configuration of .htaccess and I'll take a look.
The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.
No routing required!
enter code here
<?php
class Blog extends Controller {
function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->show_post();
}
}
function index()
{
// show blog front page
echo 'blog';
}
function edit()
{
// edit blog entry
}
function category()
{
// list entries for this category
}
function show_post()
{
$url_title = $this->uri->segment(2);
// get the post by the url_title
if(NO RESULTS)
{
show_404();
}
else
{
// show post
}
}
}
?>

Categories