Codeigniter passing "Name/Name" as one parameter in a url - codeigniter

class Barcode extends CI_Controller
{
function index($barcode, $text='', $format="PNG", $quality=100, $width=160, $height=80, $type=1)
}
I have the following function and I want to pass the following in the URL:
http://localhost/index.php/barcode/index/1/Test%2FTest/PNG/100/256/80/1
But I get a 404 when trying to do this.

Don't you need to get all the parts of the GET request via $this->uri->segment('{n}') first?
I might be wrong, but I don't think the way you are doing it is right. If I was to code this I would do the following:
function index()
{
$barcode = $this->uri->segment('3');
$text = $this->uri->segment('4');
$format = $this->uri->segment('5');
$quality = $this->uri->segment('6');
$width = $this->uri->segment('7');
$height = $this->uri->segment('8');
$type = $this->uri->segment('9');
// continue with your code
}

Related

Method behavior with codeigniter parameters

Needing help understanding codeigniter 3 behavior
I'm not understanding the controller behavior very well and I ask for help
class Simulado extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
//permissão para acessar app
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados = '0'//todas as materias;
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
public function materia () {
$dados = 1; //idamateria
$bdinsert = $this->simulado->set_bd($dados);
$this->load->view('prova',$dados)
}
}
model
public function set_bd ($dados) {
$data = [
'id_mat' = $dados
];
$this->db->insert('simulados', $data);
}
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = FALSE;
$route['app'] = "/app/home";
$route['site'] = "/home";
when accessed through the link http://localhost/escola/app/simulado, the system behaves in the desired way, it writes the value zero once in the database, for example.
accessing the url we have the method of the class, simulated, http://localhost/escola/app/simulado/materia/1/exatas-matematica calls the method in the correct way, but when writing to the database it writes the code of the matter.
I did a test and put the same code of the article method in the index and called the url, and it worked perfectly, writing only once the code of the article in the database.
I need to know why it is recording 3 times in the database when accessing the url is a method of the class and has parameters.

Laravel 6.2 - Dynamically Call a Controller action

I have used a code from Internet to call controller action dynamically. Here is the code for that, and is used in web.php. But I dont fully understand what it does.
Route::match(['get', 'post'], '{controller}/{action?}/{params1?}/{params2?}', function ($controller, $action = 'index', $params1 = '',$params2 = '') {
$params = explode('/', $params1);
$params[1] = $params2;
$app = app();
$controller = $app->make("\App\Http\Controllers\\" . ucwords($controller) . 'Controller');
return $controller->callAction($action, $params);
})->middleware('supadminauth');
Can someone explain?
Route::match(['get', 'post'], '{controller}/{action?}/{params1?}/{params2?}', function ($controller, $action = 'index', $params1 = '',$params2 = '') {
The first line looks at the request to see whether it is a get or post request, if it is some other types of request that means it does not match and will not proceed further. Then the url are separated into 4 parts corresponding by their name and passed into variables with the same name i.e. $controller, $action, $param1 and $params2 where the last 3 variables do not need to be present (with ? at the end of the name).
$params = explode('/', $params1);
$params[1] = $params2;
I believe this is a crude way to create an array of parameters as $params where the following would be more appropriate.
$params = [$params1, $params2];
.
$app = app();
$controller = $app->make("\App\Http\Controllers\\" . ucwords($controller) . 'Controller');
Then load the relevant controller.
return $controller->callAction($action, $params);
And run the corresponding action and passing all the parameters with it.
Hope this makes sense.
This is example of use it:
If you have controller like bellow:
class AdminController extends Controller {
public function index(){ //sample 0, sample 1
...
}
public function view($param1){ //sample2 , sample3
...
}
}
There is some sample route for calling them
sample0: yoursite.com/admin
sample1: yoursite.com/admin/index
sample2: yoursite.com/admin/view
sample3: yoursite.com/admin/view/5
Notice in your question ? in {action?} means it can either have value or not. Other things is simple and clear. Do you need more explaination?

how can i get language value randomly in laravel controller?

class DynamicDependent extends Controller
{
function fetch(Request $request)
{
$value = "home";
$value2 = Lang::get('home.'.$value.'');
}
}
output :'home.home'.
But i need value from language file.
please guide me to get this.
It seems like you are trying to get a translation. For that you can use the trans helper method like this:
//In your resources/lang/{some_lang_code}/home.php
return [
'home' => 'My translation',
];
//In your controller
$value = "home";
$value2 = trans('home.'.$value); //My translation

Loading view inside a Library, issues with cached vars

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.
Here is how I have structured my code:
My Controller:
class Page extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
...
$this->load->library('widgetmanager');
}
public function index($slug = '') {
echo $this->widgetmanager->show(2);
echo $this->widgetmanager->show(1);
}
}
My Library
class WidgetManager
{
private $CI;
public function __construct()
{
$this->CI = & get_instance();
}
public function show($widget_id) {
$data = array();
$widget_id = (int)$widget_id;
$this->CI->db->select('*');
$this->CI->db->from('widget');
$this->CI->db->where('id', $widget_id);
$query = $this->CI->db->get();
$item = $query->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
$data['widget_title'] = '';
$data['widget_content'] = '';
$this->CI->load->view('widget/'.$item['source'], $data);
return $widget;
}
}
widget 1: Calls widget/content
widget 2: Calls widget/banner
What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.
I have tried:
Using clear_vars(): $this->CI->load->clear_vars(), before and after doing load->view on the library.
Calling load->view with empty array, null, etc
Tried to add a prefix with the widget slug to the vars (that works, but I have to send in some way the prefix to the view, so it is not possible due cache issue)
Any ideas?
Here is what should work.
(I took the liberty of simplifying your database call making it require much less processing.)
public function show($widget_id)
{
$data = array();
$widget_id = (int) $widget_id;
$item = $this->CI->db
->get_where('widget', array('id' => $widget_id))
->row_array();
$data['widget_title'] = $item['title'];
$data['widget_content'] = $item['content'];
$widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);
//clear the cached variables so the next call to 'show()' is clean
$this->CI->load->clear_vars();
return $widget;
}
On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

codeigniter ftp get filesize

How can i get remote file's file size with codeigniter?
after making a connection , i want to get properties.xml file's size. in ordinery ftp i would get it with no problem but since i start using codeigniter it is kinda difficult.
this is in procedurel php ;
$remote_filesize = ftp_size($conn_id, "properties.xml");
but how can i do it in codeigniter?
if($this->ftp->connect($config))
{
/*here some magic code*/
}
this a small example
Controller_ftp.php
class Controller_ftp extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('ftp');
}
publilc function get_ftp_info(){
$config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;
foreach($this->ftp->list_files('/public_html/') as $item){
$size = filesize($item);
echo "name => $item - size => $size - etc <br>";
clearstatcache();
}
}
}
If you want to use $this->ftp->file_size($file), you need to extend ftp library and create the function retunring filesize($item);
ok i went to library->ftp.php and basically added a function
public function get_conn_id()
{ return $this->conn_id;}
and later all i need to do was calling the function like this
$conn_id = $this->ftp->get_conn_id();
$remote_filesize = ftp_size($conn_id, "properties.xml");

Resources