codeigniter get value from url - codeigniter

How can i recieve the value in a controller from the following URL in codeigniter
http://localhost/directory/c_service/get_radius/lang=123
controller:
class C_service extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function get_radius()
{
i need to get the vLUE 123 here
like,
`$value=$get['lang'];`
}
Thanks

You can use:
<?php
$this->input->get('lang', TRUE);
?>
The TRUE is to turn on XSS filtering, which you do want turned on.
Check out https://www.codeigniter.com/user_guide/libraries/input.html for more info.

enable url helper in config/autoload.php
$autoload['helper'] = array('url');
or load url helper in desired controller or its method
$this->load->helper('url');
and then use below in controller
$this->uri->segment(3);
the above line will get you the first parameter, increasing value will get you parameters
$this->uri->segment(4);
will get you second and so on.
hope this helps you

In Codeigniter you can simply do
public function get_radius($lang)
{
var_dump($lang); //will output "lang=123"
}
So your link could be simplified to http://localhost/directory/c_service/get_radius/123 if you don't want to do something like explode('=', $lang) to get your value.
You should however also consider adding a default value public function get_radius($lang=0) if the link is opened without a parameter.
Adding more variables is as easy as public function get_radius($lang, $other) for http://localhost/directory/c_service/get_radius/123/other

I manage it and i check it thats working
first load the url
$this->load->helper('url');
$this->uri->segment(3);
example : http://localhost/schoolmanagement/student/studentviewlist/541
if you use $this->uri->segment(3); you get (541) id
The above line will get you the first parameter, increasing value will get you parameters
$this->uri->segment(4);

Related

Laravel Ajax controller with single route

Just wondering if this is a good way to write ajax code to interact with Laravel routes?
Example my application's require to list all customer data and also list all country through ajax. I have 3 controller ApiController, CustomerController, CountryController.
So in my routes.php I have this routes
Route::get('api/v1/ajax/json/{class}/{function}', 'Api\v1\ApiController#ajaxreturnjson');
In the ApiController.php, I have below function to call other controller function to return the data I need.
class ApiController extends Controller
{
public function ajaxreturnjson(Request $request, $controller, $function){
$input = $request->input();
if($request->input('namespace') != ''){
$namespace = $request->input('namespace');
unset($input['namespace']);
}else{
$namespace = 'App\Http\Controllers';
}
$data = array();
try {
$app = app();
$controller = $app->make($namespace.'\\'.$controller);
$data = $controller->callAction($function, array($request)+$input);
} catch(\ReflectionException $e){
$data['error'] = $e->getMessage();
}
return response()->json($data);
}
}
So example to use the ajax, I just need to pass the class name, namespace and also the function name to the ajax url.
Example to retrieve all customer info.
$.ajax({
dataType:"json",
url:"api/v1/ajax/json/CustomerController/getList",
data:"namespace=\\App\\Http\\Controllers\\",
success:function(data){
}
})
So in this way, I don't have to create so many routes for different ajax request.
But I am not sure if this will cause any security issue or is this a bad design?
Personally, I would not do it this way. Sure, you could do it this way, but it's not very semantic and debugging it could be a pain.
Also, if someone else begins working on the project, when they look at your routes file, they won't have any idea how your app is structured or where to go to find things.
I think it's better to have a controller for each Thing.

How to pass variable with a master layout in Codeiginiter

My master layout here's below and working fine. I just want little bit more passing a default variable with this master layout that I can get in every pages.
class MY_Controller extends CI_Controller {
public $layout;
function __construct() {
parent::__construct();
$this->layout='layout/master';
}
}
I need to pass variable like below:
function __construct() {
parent::__construct();
$data['msg'] = $this->session->flashdata('usermsg');
$this->layout=('layout/master',$data);
}
How do I get this.
If you are loading up the data dynamically from the controller with the help of $this->layout you can send the data like this.
Method 1:
If you are using the general method to load the data to the view you can use this method.
$this->load->view('profile_view', $data);
This will load the profile_view page along with the $data as you passs into it with the help of array()
Method 2:
If you have created a master Layout and you are passing the data from the controller to the Master Layout you need to do like this.
<?php
public function master_layout () {
$this->template['header'] = $this->load->view('include/header', $this->Front_End_data, true);
$this->template['navigation'] = $this->load->view('include/navigation', $this->Front_End_data, true);
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
$this->template['footer'] = $this->load->view('include/footer', $this->Front_End_data, true);
$this->load->view('include/index', $this->template);
?>
In this code the below line alone will be loaded dynamically based on the page which you call in the master Layout.
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
In order to pass the data to this center layout you can use the funciton like this.
$data['msg'] = 'Success';
$this->template['center'] = $this->load->view ($this->middle = 'pages/view_oage',$data, true);
$this->master_layout();
And in the page you can get the data to be printed using the foreach loop as follows.
foreach($msg as $value)
{
echo $value;
}

Obtain CodeIgniter links that consider routes.php

How can I link pages in my site considering routes.php?
Example:
$route['login'] = 'user/login';
The code above allows me to see "user/login" visiting just "login". But how can I link to that page using the internal route (user/login) and get as a result the "external route" "login".
I think it's important because I could change my URLs just modifiying "routes.php" and linking everything with internal routes.
From a Drupal perspective I can have my internal route "node/1" and the external url could be "about-us". So if I use "l('node/1')" this will return "about-us". Is there a function like "drupal_get_path_alias"?
Right now I can't find anything in the CI docs that point me to the right direction.
Thanks for your help.
You could have a look at using something like
http://osvaldas.info/smart-database-driven-routing-in-codeigniter
This would allow you to have the routes configured in the database. Then if you want to dynamically create you links through a model like this:
class AppRoutesModel extends CI_Model
{
public function getUrl($controller)
{
$this->db->select('slug');
$this->db->from('app_routes');
$this->db->where('controller', $controller);
$query = $this->db->result();
$data = $query->row();
$this->load->library('url');
return base_url($data->slug);
}
public function getController($slug)
{
$this->db->select('controller');
$this->db->from('app_routes');
$this->db->where('slug', $slug);
$query = $this->db->result();
$data = $query->row();
return $data->controller;
}
}
These have not been fully tested but will hopefully give you the general idea.
I hope this helps you :)
Edit------------------------------
You can create a routes_helper.php and add a function like
//application/helpers/routes_helper.php
function get_route($path)
{
require __DIR__ . '/../config/routes.php';
foreach ($route as $key => $controller) {
if ($path == $controller) {
return $key;
}
}
return false;
}
$this->load->helper('routes');
echo get_route('controller/method');
This does roughly what you want although this method does not support the $1 $2 etc vars that can be added to reflect the :num or :any wildcard that exist. You can edit the function to add that functionality but this will point you in the right direction :D
You can do that with .htaccess file:
Redirect 301 /user/login http://www.example.com/login

How to use session_data in codeigniter throughout Website without including in particular view

Is there any other best way to use session_data in website.
How i set session in my project:
$sess_array = array('id' => $row->user_id,'name'=>$row->user_name,'email'=>$row->email,'condition'=>'','balance'=>$row->balance,'did_alloted'=>$row->did_alloted,'create_date'=>$row->create_date);
$this->session->set_userdata('logged_in', $sess_array);
when it comes to controller:
$data['id'] = $session_data['id'];
$data['name'] = $session_data['name'];
$data['email'] = $session_data['email'];
$data['balance']=$session_data['balance'];
$data['did_alloted']=$session_data['did_alloted'];
$data['create_date']=$session_data['create_date'];
$this->load->view('san-reception', $data);
and in my view. I use <?php echo $name ?> to get session_data.
So is there any method by which i can directly access session_data in view without including as $data.
$this->session->userdata('logged_in'); will be available directly in views and hence you just need to assigned this to a variable and then use that as array like bellow.
$userdata=$this->session->userdata('logged_in');
now variable $userdata contain all array field that you set in controller and hence you can use it as $userdata['id'], $userdata['name'] etc
public function __construct()
{
parent::__construct();
$data['session_data']=$this->session->user_data('logged_in');
}
public function some_function(){
$data['dummy']="";
$this->load->view('someview3',$data)
}
public function some_function1(){
$data['dummy']="";
$this->load->view('someview1',$data)
}
public function some_function2(){
$data['dummy']="";
$this->load->view('someview2',$data)
}
If you assign that into the constructor then it will call by automatically whenever a function calls in the controller.
you can view it in you view page by,
print_r($session_data);

Cannot load a model in codeigniter

I failed to load a model from my controller
This is the controller file, article.php:
<?php
class Article extends CI_Controller {
function show($id) { //id'ye gore getir
$this->load->model('articles_model');
$parameter = $this->articles_model>getarticle($id);
$this->my_template->build('article_view', $parameter);
}
}
?>
This is the model file, articles_model.php:
<?php
class Articles_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function Getarticle($id) {
parent::Model();
$query = $this->db->get_where('articles', array('id' => $id));
$data['articles'] = $query->result();
return $data;
$query->free_result();
}
}
?>
just to add, i even tried to load it from autoloader, still no chance, i assume something is wrong with the model, or the whole system broke.
up: the models loads without problems, if i put echo in __construct function, it works, however, i cannot call the getarticle function. geez
UP: I did it! according to http://grasshopperpebbles.com/codeigniter/codeigniter-call-to-a-member-function-on-a-non-object/
i used
$CI =& get_instance();
and called the function
$CI->articles_model->getarticle($id) and it called the function
It should be,
$CI =&get_instance();
$CI->load->model('articles_model');
$parameter = $CI->articles_model>getarticle($id);
There's a parse error in the following line:
$parameter = $this->articles_model>getarticle($id);
It should be:
$parameter = $this->articles_model->getarticle($id);
Does that fix your problem? If not, what error message are you seeing?
Leif's answer is the right one. Just to add one thing: you don't have to use the long variable name like $this->articles_model over and over, by using the second parameter:
$this->load->model('articles_model','artm');
$parameter = $this->artm->getarticle($id);
Just a little faster to type, and can reduce typos like the one in your sample.

Resources