Catch url in CodeIgniter - codeigniter

I am getting url like http://localhost/webpt/ipn/checkout/?token=EC-2YD51592ET0280122&PayerID=VNH3J2KQEK8AS and want to catach in my controller. in my controller code
function checkout($token = array()) {
echo"<pre>";
print_r($token);
echo"</pre>";
}
but it show empty array.

Ok I just set $config['uri_protocol'] = 'AUTO'; in config.php & use
echo ($_GET['token']); or print_r($this->input->get()); // print all the get values & it work fine thanks to all.

you can grab value of token by $this->input->get('token'); since it is passed in url following a question mark.

if you mean that you want to catch the param token,
you have two options :
format your url to be like
http://localhost/webpt/ipn/checkout/nouman
and you catch it in your controller like this :
function checkout($token) {
echo $token;
}
or use $this->input->get('token')
function checkout() { // http://localhost/webpt/ipn/checkout/?token=8767&param2=333
echo $this->input->get('token'); // echo the name param
print_r($this->input->get()); // print all the get values
}

Related

How i can get the variable data as string like a dd() function in Laravel?

I need to get the request data but i cant get ip, fullUrl and others with all() method (this only print input values), but when i use "dd(request())" this show me all data (i need the data what is printed with dd method, but like a string to save, withour the exception who print this data). Im debbuging my app so i need to save every request data in a log file, something like:
\Log::debug($request)
So,
You can use:
\Log::debug($request->toString());
or alternatively you can use
\Log::debug((string) $request);
The Laravel Request object comes from Illuminate\Http\Request which extends Symfony\Component\HttpFoundation which exposes the following code:
public function __toString()
{
try {
$content = $this->getContent();
} catch (\LogicException $e) {
return trigger_error($e, E_USER_ERROR);
}
$cookieHeader = '';
$cookies = [];
foreach ($this->cookies as $k => $v) {
$cookies[] = $k.'='.$v;
}
if (!empty($cookies)) {
$cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n";
}
return
sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n".
$this->headers.
$cookieHeader."\r\n".
$content;
}
__toString() is considered a magic method in PHP.
The __toString() method allows a class to decide how it will react
when it is treated like a string. For example, what echo $obj; will
print. This method must return a string, as otherwise a fatal
E_RECOVERABLE_ERROR level error is emitted.
You can read more about it in the official documentation.
I highly recommend to store just what you want from request data if you don't need all of them, however for both cases you can take a look at serialize and json_encode

Can I use variable in redirection function?

Can I use codeigniter redirects function contains variable name like this?
redirect($page_url);
I have page url in a session variable so, im saving session in codeigniter varible
$page_url=$_SERVER['REQUEST_URI'];
$page_url=explode("/", $page_url, 3);
$data = array('page_url'=>$page_url[2],'validated' => true);
$this->session->set_userdata($data);
Yes you can use variables surely but make sure you are loading URL helper first and checking if the $page_url variable is set. For example:
$this->load->helper('url');
if(isset($page_url) && $page_url != '') {
redirect($page_url);
} else {
redirect('/index.php');
}

CodeIgniter URL_TITLE model?

Here is example of URl_title CI, i know this code is do this
$title = "Whats wrong with CSS";
$url_title = url_title($title, '_', TRUE);
// Produces: whats_wrong_with_css
But hot to revers, is there a function in Ci to reverse something like this and return the true value?
like this ?
// Produces: Whats wrong with CSS
hi you can do it just with simple way
$title = ucfirst(str_replace("_",' ',$url_tilte));
echo $title;
I would "extend" CI's URL helper by creating a MY_url_helper.php file in application/helpers and create a function similar to what umefarooq has suggested.
/*
* Un-create URL Title
* Takes a url "titled" string as de-constructs it to a human readable string.
*/
if (!function_exists('de_url_title')) {
function de_url_title($string, $separator = '_') {
$output = ucfirst(str_replace($separator, ' ', $string));
return trim($output);
}
}
Providing you have loaded the url helper, you will then be able to call this function throughout your application.
echo de_url_title('whats_wrong_with_css'); // Produces: Whats wrong with css
The second ($separator) paramater of the function allows you to convert the string dependent on whether it's been "url_title'd" with dashes - or underscores _

change any rule in codeigniter to match function

I have set up my routes.php to suit the nature of my site. Unfortunately, there is a problem with my last route i.e.:
$route['(:any)'] = "profile/profile_guest/$1";
If the username password name passed is correct, for e.g. domain.com/username, it will load his/her data. if not, it loads the page with errors (because failure to retrieve data with non-existent user in database). This is my problem! I want to avoid this error showing.
Is there anyway I could avoid this error from happening? None of the echoes seems to be printing or the redirect neither is working. Don't know why! it is as if the code inside this method is not executing and the view is loaded instead. below is part of the profile_guest function:
public function profile_guest($username)
{
$username = $this->uri->segment(1);
//echo "Hello " . $username;
redirect('main', 'refresh');
if($username != '')
{
/* echo "<h1>HELLO WORLD SOMETHING</h1>"; */
It's hard to say without seeing the rest of the code.
Maybe you need to check the value before running the query:
// user_model
function get_user($username = NULL){
if($username){
return $this->db->query(...
}else{
return false;
}
}
Then check that the query returned anything before loading the view
if($this->user_model->get_user($username){
//show the page
}else{
echo "no user found";
}

Redirect to show_404 in Codeigniter from a partial view

I am using the HMVC pattern in CodeIgniter. I have a controller that loads 2 modules via modules::run() function and a parameter is passed to each module.
If either module cannot match the passed paramter I want to call show_404(). It works, but it loads the full HTML of the error page within my existing template so the HTML breaks and looks terrible. I think I want it to redirect to the error page so it doesn't run the 2nd module. Is there some way to do that and not change the URL?
Is it possible to just redirect to show_404() from the module without changing the URL?
Here is an over simplified example of what's going on:
www.example.com/users/profile/usernamehere
The url calls this function in the users controller:
function profile($username)
{
echo modules::run('user/details', $username);
echo modules::run('user/friends', $username);
}
Which run these modules, which find out if user exists or not:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
show_404(); // This seems to display within the template when what I want is for it to redirect
else
$this->load->view('details', $details);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
show_404(); // Redundant, I know, just added for this example
else
$this->load->view('friends', $friends);
}
I imagine there is just a better way to go at it, but I am not seeing it. Any ideas?
You could throw an exception if there was an error in a submodule and catch this in your controller where you would do show_404() then.
Controller:
function profile($username)
{
try{
$out = modules::run('user/details', $username);
$out .= modules::run('user/friends', $username);
echo $out;
}
catch(Exception $e){
show_404();
}
}
Submodule:
function details($username)
{
$details = lookup_user($username);
if($details == 'User Not Found')
throw new Exception();
else
// Added third parameter as true to be able to return the data, instead of outputting it directly.
return $this->load->view('details', $details,true);
}
function friends($username)
{
$details = lookup_user($username);
if($friends == 'User Not Found')
throw new Exception();
else
return $this->load->view('friends', $friends,true);
}
You can use this function to redirect 404 not found page.
if ( ! file_exists('application/search/'.$page.'.php')) {
show_404(); // redirect to 404 page
}
its very simple , i solved the problem
please controler name's first letter must be capital e.g
A controller with
pages should be Pages
and also save cotroler file with same name Pages.php not pages.php
also same for model class
enjoy

Resources