Cannot read parameter $1 using CodeIgniter URI Routing - codeigniter

I have strange problem. I have URI routing in my config CI like this:
$route['content/:num'] = "content/read/$1";
I also have controller class "content" with "read" method like this:
class Content extends CI_Controller {
public function read($id) {
echo $id;
}
}
And when I type this on browser localhost/myweb/index.php/content/1
It always echoing "$1" not 1.
Thank you for kindly help

in your route you missed the brackets around :num
$route['content/(:num)'] = "content/read/$1";

Related

problem in laravel route on tackling the response of payment gateway

I'm using laravel and integrate the payment gateway.when i send the post request then after success or cancellation ,it redirect the referece id like this
http://localhost/multi_vendor/user/paypal/return?flwref=FLW-MOCK-d4f7572650fbe61ecff7fb17a7129859&txref=rave-bxw7c98dymo8so0kwosco0wwscs8ogc
so how can tackle it in laravel?
I made route for this
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
You can create a route as you write above:-
Route::get('/paypal/return', 'User\PaypalController#payreturn')->name('user.payment.return');
Then in User\PaypalController Controller method payreturn, you can do as following:-
<?php
namespace App\Http\Controllers\User;
use Illuminate\Http\Request;
class PaypalController extends Controller
{
public function payreturn(Request $request)
{
//Here you can get the response request values
$ref = $request->flwref;
$TxRef = $request->txref;
}
}
I also didn't understand well your question, but let me try.
Possible appointments:
if your route is like localhost/multi_vendor/user/paypal/return, you must represent all steps in route file, something like Route::get('/multi_vendor/{user}/paypal/return,[...]). If you are already using somethink like it or a group with prefix, ignore it;
if you wish to redirect, you could use return redirect('yourCompleteUrl') in your controller or in route file;
if you wish to retrieave the parameters Tim Lewis anserwed what you need:
public function payreturn(Request $request)
{
$flref = $request->input('flref');
$txref = $request->input('txref');
}
#darnish manzoor just add your url callback rootverto verifycrsf token
if your redirection url is /ravecallback, just add it and it will stop giving you method not allowed
protected $except = [
'/ravecallback'
];

How to pass a variable from one controller to another laravel

I'm trying to pass a variable from a controller to some other controller. I'm doing it like this-
SomeController
public function index(){
$var = 'variable';
return redirect()->route('toAnotherController')->with('var', $var);
}
Route.php
Route::get('/anotherController', 'AnotherController#index')->name('toAnotherController');
Another Controller
public function index(){
echo $var;
}
But, this is giving an error "Undefined variable $var".
What is going wrong here?
Is there any other way to do this?
This will help you:
Route::get('/anotherController/{var}', 'AnotherController#index')->name('toAnotherController');
public function index($var){
echo $var;
}
Then use Redirect:
return redirect()->route('toAnotherController',[$var]);
Good Luck ~~~
I hope this is not too late. But I am sure this might help somebody one day!!!
In your SomeController
Add this instance public $attributes;
Add the variable you want to pass to another controller with this
$request->attributes->add(['var' => $request->var]);
You can equally resolve it with the code below on the (Another Controller)controller where you needed it
$var= app('request')->get('var');
N:B - Laravel 5.7. I am not sure of earlier versions

CodeIgniter URI Routing: remove method name index from URL

I am working with CodeIgniter (V:2.2.6) and I have a simple class User with some basic methods like create, update, edit, delete and index. For the index function, I am using an argument $user (which is the second URI segment) in order to display some information regarding that user. So the default URL looks like:
/user/index/john
to display some information about the user 'john'.
Now, I want to remove the term 'index' from the URL, so that it looks like:
/user/john
For that purpose I have added the following rule in routes.php.
$route['user/(:any)'] = "user/index/$1";
It serves the purpose, but it prevents accessing other functions like /user/create and goes inside /user/index automatically. To solve this problem, I can not see any other way except manually adding routing rules like
$route['user/create'] = "user/create";
for each method of the User class, which is not cool at all. So, please can anyone suggest me a better way of routing under the current circumstances?
Here are my codes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index($user = '') {
echo "index!";
}
public function create() {
echo 'create!';
}
}
Note: I have gone through the CodeIgniter documentation for URI Routing and another similar question here, but could not figure out a promising solution. And please don't suggest for CodeIgniter version update.
I'm not sure, if this is the answer you like, but i think it should work.
Just put this function in your user controller.
public function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index($method);
}
}

how to debug data query in laravel

This is a beginner question, but I searched for an hour and couldn't find an answer.
I am trying to write a simple data query which I included in my HomeController
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
}
$programs=DB::table('node')->where('type', 'Programs')->get();
$programs is undefined so I am guessing that my query didn't work but I have no idea how to debug it. I tried installing firebug and phpbug and chromphp tools but they don't seem to show anything. My apache log doesn't show anything either. Am I missing something? How do I debug this?
You can't use an expression outside of a method when using a class, instead you need to put it inside a method like:
class HomeController extends BaseController {
public function getPrograms()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
// pass the $programs to the programs view for showing it
return View::make('programs')->with('programs', $programs);
}
}
So, for example, if you have a route like this:
Route::get('/programs', 'HomeController#getPrograms');
Then you may use an URL like: example.com/programs to invoke the getPrograms method in the class HomeController.
Probably this answer doesn't help much but I think you should learn the basics (PHP Manual) first so read books and articles online and check the Laravel website to read the documentation.
You can pass the result of that query to the view like so:
class HomeController extends BaseController {
public function showWelcome()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
return View::make('hello', array('programs' => $programs));
}
}
And in your view you will have access to the $programs variable.
I don't know If i'm missing the point, but can't you use the "dd($programs)" to check what is or not is inside the variable?

Can't call method from URL when super class is REST_Controller, codeigniter

I want to run URL from browser and get file content located out of my project, for example: /home/fessy/file.txt
I use codeigniter and doctrine.
I have a method:
class Api extends REST_Controller {
// ....
public function get_file_content_get(){
var_dump(array('ftp_path' => $this->uri->segments));
$name = $this->uri->segment(3);
$rootPath = $this->uri->segment(4);
$file = file_get_contents("$rootPath/$name.txt", FILE_USE_INCLUDE_PATH);
// $this->_response('success', array('file_contents' => $file));
}
So I generate my URL as:
site.com/index.php/api/get_file_content_get/118130/%2Fhome%2Ffessy%2Ffile.txt
But I get an error:
<xml>
<status/>
<error>Unknown method.</error>
</xml>
When I change super class from REST_Controller to CI_Controller I succeeded to call get_file_content_get method.
I event can't get site.com/index.php/api/get_file_content_get/ to call the index()
What I'm doing wrong?
thanks,
Found the issue:
I need to remove _get postfix since Doctrine uses this postfix as GET indicator
Instead:
site.com/index.php/api/get_file_content_get/118130/%2Fhome%2Ffessy%2Ffile.txt
should be:
site.com/index.php/api/get_file_content/118130/%2Fhome%2Ffessy%2Ffile.txt

Resources