Yii2 - Get Current Action ID in main.php - yii2

In the file main.php at views/layouts/main.php, I want to add few classes to the body tag and add css class 'active' to the navigation bar items according to the view being displayed. Is there any way I can get action id in main.php?
I hope that html is not req

Or you can use:
Yii::$app->controller->id // get controller name
to access the controller, and:
Yii::$app->controller->action->id // get action name
for getting the current action id. But put this directive at the top of file:
use Yii;

In a layout you can access the current controller via $this->context, and so the current action id by $this->context->action->id

Related

Create a FormGroup Object via My Form inputs

I have created a Form called compaign! My purpse is to register Form's inputs data within a FormGroup Object and send it to my backend ! The first thing I have done it ; is that I created a FormGroup model i used the class CompaignForms , then i had to instanciate this class using a local variable called compaignexample ; After that i have created the main FormGroup object called compaignform and i had to initialize it with compaignexample attributes using get method ! But the problem is in my template becauz i
worked with formControl and ngModel at the beginning ! So i was urged to delete the directive[(ngModel)] and i kept onlyformControlName! However , the console displays that i have to make a parent component ( [formGroup] ) ! I tried it in vain !! Please how can i fix that problem !
I mentionned my stackblitz demo and it doesnt contain the whole project !! there is a path problem there ! but the only problem which figure out currently is the one above !!
enter link description here
remove this code #compaignForm="ngForm" from your form tag and add this [formGroup]="compaignexample" to your form tag.
<form [formGroup]="compaignexample" (ngSubmit)="saveCompaign()"></form>
ngForm is used when creating Template-driven forms.

how to create a gloabal variable in angularjs?

I'm trying to develop a shopping cart where I have list of products , when user clicks on each product details will be displayed ,I want to add each product to global map or something when user clicks the add to cart button ,how can I achieve this?
for better understanding here is my link to plunker: https://plnkr.co/edit/oo05d6H6AxuJGXBAUQvr?p=preview
This is my details page where I want to provide user with add to cart option:
<!DOCTYPE html>
<p>ItemName: {{itemName}}</p>
<p> ItemPrice: {{itemPrice|currency}}</p>
<p>ItemRating:{{itemRating}}</p>
<img src="{{itemImage}}">
<p><input type = "submit" value = "Add to Cart" ng-click = "addProduct()"></p>
In your case a simple place to store the cart in the main controller, as it is accessible from the child controllers of the nested views. So here is how the addProduct() function could be implemented:
app.controller('mobileController', function($scope) {
$scope.items = ...
$scope.cart = [];
$scope.addProduct = function(item) {
$scope.cart.push(item);
}
});
and so the function could be called from the product details controller, provided you allow access to the viewed item in it:
<input type= "submit" value="Add to Cart" ng-click="addProduct(item)">
Since then you can display your updated cart in your main controller view (or in a child view of it, as it remains accessible):
<div ng-repeat="item in cart track by item.name">
<li>{{item.name}}</li>
</div>
Should you want to add the same item multiple times, you'll have to display orders (or product serial numbers) instead of products.
Find the forked plunker here.
P.S.: Another option would be to store the cart in an Angular service, which would be injected in any controller that needs it.
P.P.S.: I kept on using the $scope as you did, but you should update your code to the latest Angular usages, such as using the controllerAs syntax and using controllers classes that can refer this (and a controller's name prefix in the templates) instead.
Try this version.
There is acartService defined here and injected in your ItemCtrl. All items are stored in a cart array inside the cartService.
Be aware that this works until you refresh the page. If you want to persist this cart variable so that it doesn't get erased during page refresh, you should look into localStorage or $cookies.

code igniter dynamic routing

I am using code igniter.
What I want to do is,
if a page is visited that does not exist
example.com/idontexist
Then I want to first check a database to see if idontexist is in the database.
If it is then I want to route the user to
example.com/action/idontexist.
How can I do this?
I feel like this gets asked every week.
Open up your application/config/routes.php, then add something like this:
$route['^(:any)'] = "my_controller/get_article/$1";
Please note that it will route everything to a controller called action. If you have other controllers then you should add a route for them too (preferably placed before this one).
// EDIT: Using this you can goto http://your-site.com/secrets-of-internet-marketing and it will call the get_article function in the my_controller controller, and pass "secrets-of-internet-marketing" as the first argument. Which can then process with something like this:
public function get_article($article_name) {
// something like this:
$article = $this->article_model->get_model_by_name($article_name);
$this->load->view('article', $article);
}
One solution would be to extend the CI_Router class with your own in application/core/MY_Router.php and just copy the method _set_routing() in your class.
Your changes would go somewhere after routes.php file is included and set to $this->routes property, you can add your custom routes.
include(APPPATH.'config/routes'.EXT);
...
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
unset($route);
//Get your custom routes:
$your_routes = $this->_get_custom_routes();
foreach($your_routes as $custom_route)
{
$this->routes[$custom_route['your_regex_match']] = $custom_route['controller'].'/'.$custom_route['action'];
}
Of course you might not need this, but I hope it gives you some idea.
This way you can add custom routes and since you will have to fetch them from database, consider caching them for better speed.

How do you set up a page to use a different template in Zotonic?

I would like to have alternative templates for each section of my website.
How do you set up a page to use a different template in Zotonic?
Here is how you could render the about page with a custom template:
{about, ["about"], resource_page, [ {template, "about.tpl"}, {id, page_about}]}
If you add the above dispatch rule to your site's dispatch rules (found at yoursite/dispatch/dispatch) it will render a page with the unique name of page_about using about.tpl.
You can set the unique name for a page in the advanced tab of the page in the zotonic admin.
Good practice is to set the page_path property to the url you define in the dispatch rule. So that the page_url of the (in the example) page_about is also "/about".

ASP.Net MVC: How to dynamically generate a meta tag based on the content of the url?

Here is the idea:
When the user wants to see /controller/action, then I want the page to have a "robots" meta tag with its value set to "all".
When the user wants to see /controller/action?sort=hot&page=2 (i.e. it has a query string), then I want the page to have a "robots" meta tag with its value set to "noindex".
Of course this is just an example and it could be another tag for the existence of this question.
At what stage in the MVC architecture could I place a hook so that the view generates the tag I want in the master page?
I do something similar for generating page titles and it works well. Put your tag in your masterpage as normal:
<%= Html.Encode(ViewData["Title"]) %>
Then subclass ActionFilterAttribute and override OnActionExecuting. From there you get access to the controller context and can set your viewdata to whatever you want.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewData["title"] = "whatever";
}
last step is to put Attribute your controllers that you want to use the filter context. You can inherit from a base controller if you want to add the attribute to all classes. There are also overloads if you want to pass parameters. In my app. I actually pass the page title.
Hope that helps.