Yii: CMenu items for different module - yii

I'd like to make a menu in Layout which the items are linked to other different module.
e.g:
Item "Product" linked to an action in Product Module, item "Service" linked to an action in Service Module.
It won't work when I set the 'url'=>('product/<controllerID>/<actionID>') and 'url'=>('service/<controllerID>/<actionID>') because once we're in Product module and click the menu "Service", the URL become
index.php?r=product/service/<controllerID>/<actionID>
instead of
index.php?r=service/<controllerID>/<actionID>
and it will be 404 error. (for sure, because the Service Module isn't inside Product Module but the URL makes it looks like that).
Any solution for this?

Check the createUrl() documentation :
the URL route. This should be in the format of 'ControllerID/ActionID'. If the ControllerID is not present, the current controller ID will be prefixed to the route. If the route is empty, it is assumed to be the current action. If the controller belongs to a module, the module ID will be prefixed to the route. (If you do not want the module ID prefix, the route should start with a slash '/'.)
That last line tells us everything. Best thing to do for you is start all the routes with a / :
'url'=>array('/<moduleID>/<controllerID>/<actionID>')

Check this
'url'=>$this->createUrl('/<moduleId>/<controllerID>/<actionID>')

Related

TYPO3 9.5 LTS route enhancer with f:select

I'm using a simple extension, which displays a list of properties from a domainobject in a f:select. After using the dropdown the form redirects to an controller action and the repository gets all records according to the choosen property by argument.
<f:form class="filter-select" name="filter-select" method="post" action="showByProperty" pageUid="{settings.detailShowByProperty}">
<f:form.select name="filter-form" options="{properties}" optionLabelField="title" optionValueField="uid" prependOptionLabel="Please choose..." prependOptionValue="0" />
<button>
Show matching records
</button>
How can i use route enhancers with select field to create an url like:
http:www.mysite.com/detailpage/property
Especially i dont know how to append the property / argument.
Thanks in advance!
This wont't work. Sending an form is completly handled by client / browser. Only option is to send the form to another action which creates an redirect with uriBuilder and form data as parameters to get an speaking url.

How to implement backoffice controller

after lot of google searches and going through prestashop's official documentation over and over again, I still couldn't find an example of backoffice controller. I even looked into the modules folder of prestashop's installation, but couldn't find any.
I need to implement 3 different back-office pages, each served by its own controller and view.
Can anyone provide me any hint, or even one working example...Just hello world is more than enough.
Thanks in advance...
notice:i write this article for prestashop1.5 and i don't check it for prestashop 1.6.perhaps it is works for 1.6 too.
You should know every thing in this way have special structure.
step 1: Create a folder in your module folder call that 'controllers' (notice:this name is static)
step 2: Create a folder in controller folder call that 'admin' (notice:this name is static too)
step 3: Create a php class file call that AdminMyclassnameController (notice:in this name Admin at first and controller at last is the key word and thay are static but Myclassname is dynamic .attention to first words all of should be uppercase A for Admin, M for Mclass,... )
step 4: Then you should write your class in AdminMyclassnameController and this class should extent with AdminController or AdminControllerCore.
for know how does it class work you can search about helper forums in internet.
step 5: When you create the class you want a tab to show that controller notice:when act to step 1,2,3,4 this controller take an automatic URL for access to this URL you should create a tab.in yourmodule.php in install() you should add this codes
$tab = new Tab();
$tab->class_name = 'AdminTest';
$tab->module = 'test';
$tab->id_parent = 9;
$tab->position = 11;
then you can see the tab in admin office that redirect to your controller.
i am tired to continue ... but if you want more send a message and i continue this article
this is use full site really clear:
http://doc.prestashop.com/display/PS15/Using+helpers+to+overload+a+back-office+template
http://presthemes.com/prestashop-news/modules-classes-and-controller-override-by-julien-breux-4.html
http://doc.prestashop.com/display/PS15/Diving+into+PrestaShop+Core+development
http://www.prestashop.com/forums/topic/270177-solved-making-own-objectmodel-class-doesnt-work/
http://doc.prestashop.com/display/PS15/New+Developers+Features+In+PrestaShop+1.5
http://blog.belvg.com/how-to-implement-a-controller.html
best regards

Completely custom path with YII?

I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need

How do you check if the current page is the frontpage using YII?

Drupal has a function called "drupal_is_front_page". Does YII have something similar to deal with navigation in this way?
Unfortunately not. And while the information needed to piece this together is available, doing so is really more pain than it should be.
To begin with, the front page is defined by the CWebApplication::defaultController property, which can be configured as discussed in the definitive guide. But there's a big issue here: defaultController can in reality be any of the following:
a bare controller name, e.g. site
a module/controller pair, e.g. module/site
a controller/action pair, e.g. site/index
a module/controller/action tuple, e.g. module/site/index
If you have specified the defaultController as #4 (which is the same as #3 if your application does not include any modules) then everything is easy:
function is_home_page() {
$app = Yii::app();
return $app->controller->route == $app->defaultController;
}
The problem is that if defaultController is specified as #1 or #2 then you have to examine a lot of the runtime information to convert it to form #3 or #4 (as appropriate) so that you can then run the equality check.
Yii of course already includes code that can do this: the CWebApplication::createController method, which can accept any of the valid formats for defaultController and resolve that to a controller/action pair (where controller is dependent on the module, if applicable). But looking at the source doesn't make you smile in anticipation.
To sum it up: you can either assume that defaultController will always be fully specified and get the job done with one line of code, or borrow code from createController to determine exactly what defaultController points to (and then use the one line of code to check for equality).
I do not recommend looking into solutions based on URLs because the whole point of routes is that different URLs can point to the same content -- if you go that way, can never be sure that you have the correct result.
In my experience, there is no such function in Yii. However, you can retrieve the followings:
base url: Yii::app()->request->baseUrl
current URL : Yii::app()->request->requestUri.
current page controller with Yii::app()->getController()->getAction()->controller->id .
With these APIs, it should be possible to find out whether the current page is front page.
another simple idea:
in your action (that one you use to present your 'main front page'), you could set up a variable using a script in its view:
Yii::app()->getClientScript()->registerScript("main_screen",
"var main_front_page = true;",CClientScript::POS_BEGIN);
put that code in the "main view", (the rest view pages dont have this piece of code).
so when you need to check if a page is the "main page" you could check for it using javascript, quering for:
if(main_front_page){..do something..}.
if you need to recognize the main page in php (in server side), use the method proposed by Jon.
another solution, based on a common method for your controller:
Your controllers all of them must extend from CController, but, when you build a new fresh yii application Gii creates a base Controller on /protected/components/Controller.php so all your controllers derives from it.
So, put a main attribute on it, named:
<?php
class Controller extends CController {
public $is_main_front_page;
public function setMainFrontPage(){ $this->is_main_front_page = true; }
public function getIsMainFrontPage(){ returns $this->is_main_front_page==true; }
}
?>
well, when you render your main front page action, set up this core varible to true:
<?php
class YoursController extends Controller {
public function actionPrimaryPage(){
$this->setMainFrontPage();
$this->render('primarypage');
}
public function actionSecondaryPage(){
$this->render('secondarypage');
}
}
next, in any view, you could check for it:
<?php // views/yours/primaryview.php
echo "<h1>Main Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? true"
?>
<?php // views/yours/secondaryview.php
echo "<h1>Secondary Page</h1>";
echo "is primary ? ".$this->getIsMainFrontPage(); // must say: "is primary ? false"
?>

Changing line item properties on cart in Shopify

Is it possible to change line item properties after they have been added to the cart? (Either via a normal form submission, or via AJAX?)
I've tried a POST to /cart/change with a "properties[MyProperty]" key, but no luck so far. This is coupled with the line parameter to denote the unique line item.
Any ideas? Or is it just a straight 'no'?
Using Shopify's API you can't use cart/change.js to change the properties of a line item. The reason is that cart/change.js uses 'properties' to find the line item you want. The API documentation omits this. Here is an example:
When I make a POST to cart/add.js with the following url encoded parameters:
quantity=9403&id=278440178&properties%5Bmy-property%5D=property%20BAR
The response will include,
"properties":{"my-property":"property BAR"}
When I go on to make a POST to cart/change.js to change the property from BAR to FOO,
id=278440178&properties%5Bmy-property%5D=property%20FOO
Then the response will include,
"properties":{"my-property":"property BAR"}
That is, I was unable to change the line item property this way. You may suspect that this is because there is some trick to the cart/change.js API, but this is not the case.
Notice, when I try to remove a line item by making a POST to cart/change.js and specifying quantity=0, like this:
quantity=0&id=278440178&properties%5Bmy-property%5D=property%20FOO
With the property property FOO being one that does not belong to any item (my cart only has an item with property BAR right now), the item is not removed from the cart. If on the other hand I do this:
POST: quantity=0&id=278440178&properties%5Bmy-property%5D=property%20BAR
The item is removed as normal.
Conclusion: in cart/change.js, shopify uses line-item properties in the same way it uses 'id', that is, to find the line item whose quantity you want to change. Just in the same way that you can't use cart/change.js to change the id of a line item, you can't use it to change the properties of one.