Removing function name URL in Codeigniter CMS - php

when data pass in controller that time the data and function name pass into URL
localhost/project/course/Web-Development
the above example course is function name of controller
but remove it and pass this URL
localhost/project/Web-Development

using remap function we can solve this problem
code in controller
public function _remap($method, $params = array())
{
if ($method == 'autocomplete') {
return call_user_func_array(array($this, $method), $params);
} else {
$methodcall = $this->M_tl_admin->Validate_Web($method);
if ($methodcall == 'course') //***course is your function name***
return call_user_func_array(array($this, $methodcall), array($method));
}
}
code in model
public function Validate_Web($alias)
{
$res = $this->db->get_where('category', array('ctg_url' => $alias))->result_array();//category is table name and ctg_url is data pass in URL(Web-Development)
if(count($res)>0)
return 'course';
}

You can use route features to hide function name.
https://www.codeigniter.com/user_guide/general/routing.html
$route['product/:any'] = 'project/product_look

Related

How to access data of one function to another in laravel

Okay the issue is something like this
I have a function in AController
public function index()
{
$store = Store::(query)(to)(rows)->first();
return view('store.index', compact('store'));
}
Now in the same controller I have another function
public function abc()
{
return view('store.abc');
}
Now to this function I also want to send the compact('store') to the view abc I can just add the query again in the abc() function but that would be lazy and make performance issues. Is there a way that I can access $store object in other functions too?
If I understand you correctly you want to access the same query from two places. So extract getting stores to another method like
private function store()
{
$minutes = 10; // set here
return Cache::remember('users', $minutes, function () {
return Store::(query)(to)(rows)->first();
});
}
Additionally I have cached the query. So it get executed once at a defiened time.
Then access it from other two methods like,
public function index()
{
$store = $this->store();
return view('store.index', compact('store'));
}
public function abc()
{
$store = $this->store();
return view('store.abc', compact('store'));
}
class StoreController extends Controller
{
public function index()
{
return view('admin.store',['data' => $this->getSetting()]);
}
public function getStoreData()
{
//get your data here, for example
$data = Store::where('status',1)->first();
//get all data
//$data = Store::all();
return ($data);
}
}
Try the following. Not testing but it should work for you.
class AController
{
public function getStore()
{
$store = Store::(query)(to)(rows)->first();
return compact('store');
}
public function index()
{
return view('store.index', $this->getStore());
}
public function abc()
{
return view('store.abc', $this->getStore());
}
}

PHP - Generate Functions from array of string

In my application i need many getter and setter and my idea was to generate them from an array, for example:
protected $methods = ['name', 'city'];
With this two parameters, i will need to generate the following methods:
public function getNameAttribute() {
return $this->getName();
}
public function getName($lang = null) {
return $this->getEntityValue('name', $lang);
}
And for city, the method will be:
public function getCityAttribute() {
return $this->getCity();
}
public function getCity($lang = null) {
return $this->getEntityValue('city', $lang);
}
Sure, i should need to generate the setter too (with the same logic).
As you can see, i will need a method with get<variable_name>Attribute and inside this call get<variable_name> and the other (getName) return even the same method (for each getter) and just change the 'name' parameter.
Every method have the same logic and i would like to generate them "dynamically". I don't know if this is possible..
You can leverage __call() to do this. I'm not going to provide a full implementation but you basically want to do something like:
public function __call($name, $args) {
// Match the name from the format "get<name>Attribute" and extract <name>.
// Assert that <name> is in the $methods array.
// Use <name> to call a function like $this->{'get' . $name}().
// 2nd Alternative:
// Match the name from the format "get<name>" and extract <name>.
// Assert that <name> is in the $methods array.
// Use <name> to call a function like $this->getEntityValue($name, $args[0]);
}
Send this params(name, city or other) as parameter to universal method(if you don't know what params you can get)
public function getAttribute($value) {
return $this->get($value);
}
public function get($value, $lang = null) {
return $this->getEntityValue($value, $lang);
}
If you know yours parameters, you can use this:
public function getNameAttribute() {
return $this->getName();
}
$value = 'Name'; //for example
$methodName = 'get' . $value . 'Attribute';
$this->$methodName; //call "getNameAttribute"
Take a look at this and let me know is it your requirement or not.
$methods = ['name', 'city'];
$func = 'get'.$methods[1].'Attribute';
echo $func($methods[1]);
function getNameAttribute($func_name){
$another_func = 'get'.$func_name;
echo 'Name: '.$another_func();
}
function getCityAttribute($func_name){
$another_func = 'get'.$func_name;
echo 'City: '.$another_func();
}
function getCity(){
return 'Dhaka';
}
function getName(){
return 'Frayne';
}

How I can grab all public function methods in a controller?

I use __remap() function to avoid any undefine method and make it redirect to index() function.
function __remap($method)
{
$array = {"method1","method2"};
in_array($method,$array) ? $this->$method() : $this->index();
}
That function will check if other than method1 and method2.. it will redirect to index function.
Now, how I can automatically grab all public function methods in that controller instead of manually put on $array variable?
You need to test if method exists and is public. So you need use reflection and method exists. Something like this:
function __remap($method)
{
if(method_exists($this, $method)){
$reflection = new ReflectionMethod($this, $method);
if($reflection->isPublic()){
return $this->{$method}();
}
}
return $this->index();
}
Or you can use get_class_methods() for create your array of methods
OK, I was bored:
$r = new ReflectionClass(__CLASS__);
$methods = array_map(function($v) {
return $v->name;
},
$r->getMethods(ReflectionMethod::IS_PUBLIC));
I've modified the codes and become like this.
function _remap($method)
{
$controllers = new ReflectionClass(__CLASS__);
$obj_method_existed = array_map(function($method_existed)
{
return $method_existed;
},
$controllers->getMethods(ReflectionMethod::IS_PUBLIC));
$arr_method = array();
//The following FOREACH I think was not good practice.
foreach($obj_method_existed as $method_existed):
$arr_method[] = $method_existed->name;
endforeach;
in_array($method, $arr_method) ? $this->$method() : $this->index();
}
Any enhancement instead of using foreach?

Codeigniter _remap function is setting its own default value to the string "index"

I'm writing the back-end for a website using CodeIgniter. I want to be able to use these two URLs:
(1) website.com/product (to return data on all products)
(2) website.com/product/2 (to return data on product with e.g. ID 2).
I have a Product controller, here is the relevant code in outline:
class Product extends CI_Controller
public function _remap($id = -1)
{
$this->my_function($id)
}
public function my_function($n)
{
if ($n == -1)
{
// Code to return data on all products
}
else
{
// Code to return specific product data
}
}
I'm using _remap() because without it, when the URL is of the form website.com/product/id, codeigniter will want to interpret the id as a method. If the URL is just website.com/product then the $id variable is set to have a default value of -1. But bizarrely this doesn't happen: instead $id is set to the string "index" (I checked this by adding var_dump($id); to the _remap() function).
What is going on?
I would remove the _remap function.
class Product extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index($id = -1)
{
if($id == -1)
{
echo 'full list';
}
else
{
echo 'work on id' . $id;
}
}
}
Then create a route in config/routes.php
$route['product/(:num)'] = 'product/index/$1';
If you really want to use _remap then you'll need to do something like the following:
public function _remap($method, $params = array())
{
if($method == 'index'){
return call_user_func_array(array($this, 'my_function'), $params);
}
}

Passing variable to view from controller

Can something like this be done? I want to pass a variable from a public function to my view.
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
public function a_function() {
public $variable = "cool";
}
//EDIT//
This is what I m actually trying to accomplish and I m stuck.
get_two gets two items from a table. I want to add the two items to two variables and pass them to the view.
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
$html = '';
foreach($get_results as $result){
$html .= '<li>'.$result->brand.'</li>';
}
$result = array('status' => 'ok', 'content' => $html);
header('Content-type: application/json');
echo json_encode($result);
exit();
}
}//public function get_two() {
Should I create two functions like this? But I don't know how to pass the $get_results array from get_two to the below functions. I tried public $get_results = $this->model ... etc but that didn't work.
public function result_one() {
return $resultOne = $get_results[0];
}
public function result_two() {
return $resultTwo = $get_results[1];
}
I'm not sure I've got the question correctly but what you're trying to achieve is something like this?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->a_function();
$this->load->view('home_view', $home_data);
}
public function a_function() {
return $variable = "cool";
}
/** AFTER EDIT **/
Things get complicated (possibly because of my english comprehension).
you said
get_two gets two items from a table. I want to add the two items to two variables and pass them to the view.
So from the function get_two() you need to get and use the result in this way?
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- here?
$this->load->view('home_view', $home_data);
}
So you can try with:
public function get_two() {
$get_results = $this->home_model->get_two_brands();
if($get_results != false){
return $get_results;
}
}
and then
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two();
$this->load->view('home_view', $home_data);
}
and inside you home_view :
<?php
foreach($home_data['cool'] as $result){
echo '<li>'.$result->brand.'</li>';
}
?>
/** AFTER NEW QUESTION **/
I need the ids of the two choices as two distinct variables
So change the index function this way:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->get_two(); // <- maybe you don't need this anymore
list($result1, $result2) = $this->get_two();
$home_data['resultId1'] = $result1->id;
$home_data['resultId2'] = $result2->id;
$this->load->view('home_view', $home_data);
}
Now you're able to use $home_data['resultId1'] and $home_data['resultId1'] inside your view.
You can also define the variable in the constructor, this is one way .
CODE:
public function __construct(){
$this->variable = "cool";
}
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->variable;
$this->load->view('home_view', $home_data);
}
I don't know the codeigniter framework so this is why I asked for a part of your view but it looks pretty simple as I check in the doc. And the doc's are not bad there.
Check for Adding Dynamic Data to the View
public_function() should return something, for ex:
function public_function() {
return 'groovy';
}
Then call it in the controller:
public function index() {
$home_data['username'] = "myname";
$home_data['cool'] = $this->public_function();
$this->load->view('home_view', $home_data);
}
Then add to the view somewhere
<?php echo $home_data['cool'];?>
I assume it's wrapped in some class. So if you cannot return the value you need (for ex. function already returns something else) then do something like this:
class Someclass {
public $some_class_variable;
function public_function() {
$this->some_class_variable = 'groovy';
}
function index() {
$home_data['cool'] = $this->some_class_variable;
}
}

Categories