Can i use Model or Helper function in routes.php [codeigniter] - codeigniter

I wants to set database values to the URL.
Actually i have ID of product so that i wants to show its Name by fetching from database.
Currently i am passing integer ID through URL so that i wants a name of that ID related Product.
My current URL is like : http://example.com/controller/method/1
I wants URL like : http://example.com/controller/method/product-name

i guess u are using id as parameter in controller method.
class example extends CI_Controller {
public function method ( $product_name )
{ echo $product_name; }
}
this will give u product_name as last field in controller as like controller/method/product_name..

You can get the last bit in the url as an argument to the method of your controller class.
class myClass extends CI_Controller {
public function myMethod ( $product )
{ echo $product; }
}
This will result in url : http://example.com/myClass/myMethod/anything_goes_here
And in your controller method myMethod the argument $product will be the last part of the above url anything_goes_here
For this example, the echo in the myMethod will produce result anything_goes_here
This doesn't need rerouting.
If you are getting the products based on names then this solution will do, but I don't think this will be good in real life situation. For example what if you have more then one product with the same name ?
If you still want to use product name, then I think the following approach is better. Where you will use get request uri.
class myClass extends CI_Controller {
public function myMethod ( $product )
{ echo $this->input->get ( 'id' ); echo $product; }
}
And use the url like this : http://example.com/myClass/myMethod/anything_goes_here?id=123
In the above url, the ?id=123 is the get uri. This way you can have both the product name and the id of the product in the url.
Hope it helps.

Related

Laravel policies - passing the class as a variable to $user->can() method doesn't work

I have a route with dynamic model recognition. In other words, I take the desired model as an argument and use it in the controller. I have complex authorization in my app and I need to pass the model class name as a variable to the $user->can() method for using policies, but for some reason it doesn't work. Here's my code:
Policy:
public function view($user, Model $model) {
return $user->model_id == $model_id;
}
public function create($user) {
return $user->isAdmin();
}
Controller:
public function createModel($model) {
$model_class = $model . '::class';
if (Auth::user()->can('create', $model_class)) {
return $model_class::create();
}
return 'invalid_permissions';
}
If I hardcode the model class name it works. For example, if my model is 'Car' and in the controller I put:
if (Auth::user()->can('create', Car::class)) {
Anybody got any ideas why this is so and how to fix it? I hope that it's possible because I would have to change my whole concept if it isn't.
*Note: this is example code, not my actuall classes

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

Unable to call custom methods within extended Magento model

I'm attempting to customize the way pricing / tiered pricing is displayed in Magento CE 1.6.0.0.
I've followed the instructions in the second post of the link below to override Mage_Catalog_Model_Product_Type_Price
http://www.magentocommerce.com/boards/viewthread/16829/
Following is my custom model class:
class PHC_Price_Model_Price extends Mage_Catalog_Model_Product_Type_Price {
public function getPrice() {
echo "overridden getPrice method called<br>";
}
public function getPHCDisplayPrice($product) {
echo "custom price function called<br>";
}
}
I'm able to successfully call the overridden getPrice() function from my template file as follows:
$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPrice();
However, when I try to call my custom price function with
$product = Mage::getModel("catalog/product")->load($_product->entity_id);
$displayPrice = $product->getPHCDisplayPrice();
I get absolutely nothing. Can anyone tell me what I'm missing?
It't normal that you don't get a result. I would be amazed if this worked.
You are overriding the class Mage_Catalog_Model_Product_Type_Price, but in your example the $product variable is an instance of Mage_Catalog_Model_Product. That class does not have the method getPHCDisplayPrice and it calls the __call method and returns null.
You get the expected result when calling getPrice by accident. It is because the getPrice method in Mage_Catalog_Model_Product looks like this:
public function getPrice()
{
if ($this->_calculatePrice || !$this->getData('price')) {
return $this->getPriceModel()->getPrice($this);
} else {
return $this->getData('price');
}
}
So when you call it, it calls $this->getPriceModel()->getPrice($this) and $this->getPriceModel() returns an instance of your class.

How to make urls like category/5/sub_category in CodeIgniter

I want my urls to look like:
www.domain.com/catalog/category_name/category_id/product_name/product_id.
How should my controllers look like to accomplish this?
It's ok for the Controller to have Catalog and Function category_name in it.
But what will be my product controller and function. How can I make a structure like this.
Do I need to have a specific file structure?
I use CodeIgniter framework. Thanks for your help.
In general the controller must not be changed (if it accepts the product id as parameter).
All other information can then be put in the url from the database (querying via the product id and getting related information) through URI Routing changes.
your controller method should be like this
<?
//..
public function category_name($category_id,$product_name,$product_id){
// some cool code
}
//..
?>
As you question is very vague - so is my answer - but this function achieves what you asked
Class Catalog extends CI_Controller
{
public function category_name ($category_id, $product_name, $product_id)
{
// your function
}
}

Codeigniter not taking arguments from URL

I have a function that needs to pull arguments from the URL like CI is supposed to do. But it's not doing it. My URL is domain.com/lasers/en/acme.
My class Lasers is:
class Lasers extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('products_model');
$this->load->model('common_model');
$this->load->model('select_country_model');
$this->load->model('markets_materials_model');
}
function index($lang = NULL, $laser = NULL)
{
$query = $this->products_model->get_product_content($laser, $lang);
}
The model is loaded in the constructor. The $lang I need is "en" and the $laser I need is "acme". So why isn't this working? The arguments in the function are in the correct order, so I can't see what's wrong.
By default you cant pass arguments to the index method of a controller
if you go to domain.com/lasers/en/acme it is looking in the lasers controller for a method called en.. (which doesn't exist) and trying to pass a single parameter of acme to it
Theres a few solutions, probly the easiest is to use a different method (not index) then use routes to make the URLs work.
add something like this to your config/routes.php
$route['^lasers/(:any)/(:any)'] = "lasers/get_products/$1/$2";
Then use a method like this instead of index:
function get_products($lang = NULL, $laser = NULL) {
$query = $this->products_model->get_product_content($laser, $lang);
}
.. OR you could use _remap to override the default behaviour
Does it work if you write "domain.com/lasers/index/en/acme"?
If you write domain.com/lasers/en/acme, it will look for the "En" function, $lang being "acme", and $laser remaining NULL.

Resources