I have defined this route:
$route['importador/portes/eliminar/(:num)'] = 'mycontroller/mymethod/$id';
Now, if i enter the URL http://localhost/mywebsite/importador/portes/eliminar/4 it loads the correct controller. The content of my controller is echo $id;. And prints always "$id" instead of 4.
It's my first route so i must be doing something wrong.
Any tips?
My method on my controller "importador" is:
class importador {
public function portes_eliminar($id)
{
echo $id;
}
}
Your router should be like this:
$route['importador/portes/eliminar/(:num)'] = 'importador/portes_eliminar/$1';
Related
Here is what I want to achieve:
https://www.example.com/properties
https://www.example.com/properties/properties-in-newyork
https://www.example.com/properties/properties-in-DC/property-for-rent
https://www.example.com/properties/all-cities/property-for-rent
https://www.example.com/properties/all-cities/property-for-sale
All above is for search. Now I want to get details page like:
https://www.example.com/properties/2br-apartment-for-sale-100
I want to differentiate between search and details page links. Here is what I tried:
$route['properties/index'] = 'properties';
$route['properties(/:any)'] = 'properties/property_details$1';
How can I differential which URL is for properties/property_details function and which URL is for properties/index function?
enter image description here
Set your route.php like this :
$route['properties/index'] = 'properties';
$route['properties'] = 'properties/property_details';
$route['properties/(:any)'] = 'properties/property_details/$1';
Access url :
this direct you index method
https://www.example.com/properties/index
this will direct you property_details method
https://www.example.com/properties/
Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends CI_Controller {
public function __construtct()
{
parent::__construtct();
$this->load->helper('url');
}
public function index()
{
echo 'index';
}
public function property_details($component = NULL)
{
echo 'property_details';
echo $component;
}
}
If I'm correct, according to your explanation with differentiating the routes, the problem you are having is, it always running the route for index despite of what your URL having after properties.
You may try it by changing the order of the routes like this;
$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index'] = 'properties';
It always works according to the order of the routes you have placed. If there are acceptable parameters, for the program, properties/index is also something similar to properties(/:any). So, to differentiate between these two, we have to change the order of the routes like this.
I am trying to send a url from view page to controller but it does not seem to work the way i am thinking.
View Page
Product
User
I want to get "tbl_product"
Controller admin
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->uri->segment(4);
}
}
?>
but if the segment(4) is changed to segment(3), it shows up with displaying "product" in the screen
your controller function should have arguments for your url segments
for example:
public function test($product = 'product', $tbl = 'tbl_product') {
echo $tbl // contains the string tbl_product
}
Since you said your routes look like this:
$route['default_controller'] = "admin";
$route['404_override'] = ''
and your URL is like this:
<?= base_url() ?>admin/test/product/tbl_product
Then if your base_url() is localhost/my_app, your URL will be read as this:
http://localhost/my_app/admin/test/product/tbl_product
http://localhost/my_app/CONTROLLER/METHOD/PARAMETER/PARAMETER
So in your controller, you can do this:
class Admin extends CI_Controller {
public function test($product = NULL, $tbl_product = NULL) {
echo $product;
echo $tbl_product;
}
}
It's strange to use codeigniter for this purpose, because codeigniter uses as default the url format bellow.
"[base_url]/[controller]/[method]"
I think it will be better and more easy to just pass the values you want as get parameters and make some httaccess rules to make your url more readable for the user and robots. That said you can do that:
Product
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->input->get('product');
//should output 'tbl_product'
}
}
?>
If you prefer to use uri instead u should route your uri's so it will be like.
In your routes file you probably I'll need something like this.
$route['product/(:any)'] = "Admin/test";
This way you will probably access the uri segments correctly.
Thank you so much for going through.
$this->uri->segment(4); // is now working :S
its not working properly after all I made changes to routes.php and came back to default again. I seriously have no idea what the reason behind not displaying the result before.
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>";?>
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.
I want to pass a variable to my ajax rendering function.Something like this
$this->render('tempcomment/'.$id, 'ajax');
and my tempcomment action is
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}
but rendering is not working.
See below to start with:
public function tempcomment($id) {
// ^ ------ ...
$commentdata = $this->Post->Comment->findByPostid($pid);
// ^--- this is different!
$this->set('commentdata', $commentdata);
}
render is used just to decide what view to use in your controller action to render the action. It does not call the action itself
so if you have a code like this
public function test() {
$this->set('foo', 12345);
$this->render('tempcomment', 'ajax');
}
you are just saying that you want to use the tempcomment view to render the test action but you are not telling cake to actually execute the tempcomment action.
so if you want to call the action you have to do by yourself
$this->tempcomment($id);
$this->render('tempcomment/'.$id, 'ajax');
let me tell you that I don't see any reason to do this and I think that probably there are other ways to do what you are trying to achieve. But I assume you know what you are doing
Try this, its another work around of what you want to achieve:
public functionc ajax_tempcomment($id){ //just access this action
$this->redirect('tempcomment/'.$id);
$this->layout = "ajax";
}
public function tempcomment($id) {
$commentdata = $this->Post->Comment->findByPostid($pid);
$this->set('commentdata', $commentdata);
}