Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Problem with routing from a method to another controller
#1

(This post was last modified: 12-13-2014, 12:22 PM by g3n1u5.)

Hello guys, as I picked CodeIgniter I decided to make a small project, now I have a problem with routing, I have some pages, made step by step from the tutorial: 

I have home and addmovie as pages.

PHP Code:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = "pages/view/$1";
//$route['pages/view/addmovie'] = "movies/add";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

The url I am giving the form is the base url of the page

PHP Code:
echo validation_errors();
 
           $attr = array("class" => "col-sm-4""role" => "form");
 
           echo form_open($ADD_URL$attr); 


And now I have another Controller called Movies.php in which there is a function called add which should control the information from the form and eventually add the information to the database. The problem is I dont know how to route the files in a way that when the submit button is pressed it should redirect the form to use the method from Movies.php/add ( controller/method) !

I tried

$route['pages/view/addmovie'] = 'movies/add'; 

but it doesnt work, and I am pretty sure there is a problem with my logic. 

Best regards
g3n1u5!
Reply
#2

(This post was last modified: 12-13-2014, 07:08 AM by InsiteFX.)

The route (:any) needs to be the last route in your routes file, any routes after that will not work because it is a catch all route.

Code:
redirect('movies/add');

    // for update / edit
    redirect('movies/add/'.$id);

To update etc; You need to also pass the id of the record to the method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 12-13-2014, 07:42 AM by g3n1u5.)

It's not working!


PHP Code:
$route['default_controller'] = 'pages/view';
//$route['movies/add'] = "movies/add
//$route['pages/view/addmovie'] = "movies/add"
$route['(:any)/addmovie'] = "movies/add";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "pages/view/$1"

I tried all. Sorry if I am doing wrong or I did not understand. I am trying just to make the form to reload at the same page, and if there is an error the validation_error() is echoed if not some success message is outputted. 

The action attr is set to 
PHP Code:
<form action="http://localhost/ci/addmovie" class="col-sm-4" role="form" method="post" accept-charset="utf-8"


I managed to make it working by redirecting to another page ( as shown above - movies/add ) but I'd like to know if I can make it reload the same page. I don't want unnecessary reloads. 

P.S.

addmovie.php from the view
PHP Code:
   <!-- Main page content -->
 
       <div class="container-fluid">
 
       <div class="col-sm-4"></div>
 
       <?php
            $attr 
= array("class" => "col-sm-4""role" => "form");
 
           echo form_open($ADD_URL$attr); 
 
           echo validation_errors();
 
       ?>
              <div class="form-group">
                <input type="text" class="form-control" id="movieTitle" name="movieTitle" placeholder="Movie title" />
              </div>
              <div class="form-group">
                <input type="text" class="form-control" id="lastEpisodeWatch" name="lastEpisodeWatched" placeholder="Last episode watched" />
              </div>
              <div class="form-group">
                <input type="text" class="form-control" id="sourceURL" name="sourceURL" placeholder="Source url" />
              </div>
              <button type="submit" class="btn btn-default">Track me!</button>
            </form>
        </div>
    <!-- Main page content end --> 

movies.php - controller

PHP Code:
<?php 

class Movies extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model("movies_model");
 
   }
 
   
    public 
function add() {
 
       $this->load->helper(array('form''url'));
 
       $this->load->library('form_validation');
 
       
        $data
['title'] = "Add new movie/seris";
 
       
        $this
->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">''</div>');
 
       $this->form_validation->set_rules('movieTitle'"Movie Title""required");
 
       $this->form_validation->set_rules('lastEpisodeWatched'"Last episode watched""required|integer");
 
       $this->form_validation->set_rules("sourceURL""Source""required|valid_url|prep_url");
 
       
        $data
['BASEURL'] = base_url();              //http://localhost/ci/assets/
 
       $data['SITEURL'] = base_url();              // http://localhost/ci/index.php/
 
       $data['ASSETSURL'] = base_url().'assets/' // http://localhost/ci/assets/
 
       
        if
($this->form_validation->run() === false) {
 
           $this->load->view('tpl/header'$data);
 
           $this->load->view('pages/addmovie');
 
           $this->load->view('tpl/footer'$data);
 
       } else {
 
           $this->movies_model->add_movies();
 
       }
 
       
    
}
 
   


Pages.php - controller 

PHP Code:
<?php

class Pages extends CI_Controller {
 
   
    public 
function __construct() {
 
       
        parent
::__construct();
 
       $this->load->model('movies_model');
 
       
    
}
 
   
    public 
function view($page "home") {
 
       
        if
(!file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
 
           show_404();
 
       }
 
       
        $data
['title'] = ucfirst($page);
 
       $data['BASEURL'] = base_url();              //http://localhost/ci/assets/
 
       $data['SITEURL'] = base_url();              // http://localhost/ci/index.php/
 
       $data['ASSETSURL'] = base_url().'assets/' // http://localhost/ci/assets/
 
       $data['ADD_URL'] = base_url().'addmovie';
 
       $data['movies'] = $this->movies_model->get_movies();
 
       
        $this
->load->view('tpl/header'$data);
 
       $this->load->view('pages/'.$page$data);
 
       $this->load->view('tpl/footer'$data);
 
   }
}

?>

Don't mind some unnecessary attributes and methods, its for learning purposes only Smile 

Best regards, 
g3n1u5!
Reply
#4

(This post was last modified: 12-13-2014, 08:51 AM by InsiteFX.)

If you look at your routes file you have 2 routes with (:any)

As I stated before the any is a catch all route any route after that will not be seen!

Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

Do you have a separate movie controller and page controller?

Code:
$route['default_controller'] = 'pages';
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

The last any is necessary for the web app to work. The $route['(:any)/addmovie'] = "movies/add"; is irrelevant, it doesn't work with or without.
Reply
#6

Fixed it by putting everything in that method and managing it with bunch of ifs!

Best regards,
g3n1u5
Reply




Theme © iAndrew 2016 - Forum software by © MyBB