where we can specify default controller and action in yii - php

I have created one project in yii and my default controller points to site controller . I want to change it with some other and where i can specify default controller and action in yii.

add the configuration in the config main.php
return array(
'name' => 'Web Application',
'defaultController' => 'home',
......
);

Perfect solution for changing the default controller. Part of the question was also to change the default action. If you've set 'defaultController' => 'home', the default action will be 'index' (unless set otherwise), you can change this in the controller like so:
class HomeController extends CController
{
public $defaultAction = 'someotheraction';
public function actionSomeotheroaction()
{
}
}

You can add any where in return array protected/main.php
return array(
......
'defaultController' => 'index',
......
);
if you are working in modules base then you can add
'defaultController' => 'shop/index',
Shop is module and index is controller

You can set controller to Default Controller in project directory protected/main.php
add this code in array like $configArray = array()
$configArray = array
(
'name'=>'Web Appname',
'defaultController'=>'index'
......
);
And set the default action in Controller
class NameController extends AdminCoreController
{
public $defaultAction = 'index';
}

Related

How to set routing in Zend Framework 1.12

My aim is to have product links like:
domain.com/test-product
domain.com/second-test-product
instead of:
domain.com/products/product/id/5
domain.com/products/product/id/123
Info about each product is get in ProductsController in productAction().
It works fine:
ProductsController:
public function productAction() {
$products = new Application_Model_DbTable_Products();
$nicelink = $this->_getParam('nicelink', 0);
$this->view->product = $products->fetchRow($products->select()->where('product_nicelink = ?', $nicelink));
// nicelink is always unique
}
The link to this method looks like:
for ($i=0; $i < count($this->products); $i++) {
echo 'LINK';
}
Info about each product is displayed in product.phtml view:
<?php echo $this->escape($this->product->product_name); ?>
And my Bootstrap.php file:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initRoutes() {
$router = Zend_Controller_Front::getInstance()->getRouter();
include APPLICATION_PATH . "/configs/routes.php";
//$frontController = Zend_Controller_Front::getInstance();
$route = new Zend_Controller_Router_Route_Regex(
'(.+)',
array(
'controller' => 'products',
'action' => 'product'
),
array(
1 => 'nicelink',
),
'%s.html'
);
$router->addRoute('profileArchive', $route);
}
}
However, this solution has 1 major disadvantage: all other links such as
domain.com/contact
domain.com/about-us
are not working (probably due to the Bootstrap file).
How to fix the problem in order to make another links work and maintain current product links?
The issue is that you are matching all routes to product action in products controller. Probably you want to keep using the default routes in zend 1, which match "contact" and "about-us" to the corresponding actions in your default controller.
If you check "Zend_Controller_Router_Rewrite" function "addDefaultRoutes()" and "route()", you can see that the default routes will be checked after any custom ones.
The simplest solution, looking on what you are asking would be to match any routes that end with "-product":
$route = new Zend_Controller_Router_Route_Regex(
'(.+)\-product',
array(
'controller' => 'products',
'action' => 'product'
),
array(
1 => 'nicelink',
),
'%s.html'
);
In this case nicelink should take values "test" and "second-test" in your corresponding examples.

How to use custom routes in CakePHP?

I want to implement custom routes in CakePHP. I am following the docs
http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes
My custom route in app/Routing/Route
<?php
App::uses('CakeRoute', 'lib/Cake/Routing/Route');
class CategoryRoute extends CakeRoute
{
public function parse($url)
{
$params = parent::parse($url);
if (empty($params)) {
return false;
}
return true;
}
}
app/Config/routes.php
App::uses('CategoryRoute', 'Routing/Route');
Router::connect('/mypage/*', array('controller' => 'mycontroller', 'action' => 'view'), ['routeClass' => 'CategoryRoute']);
but I get
Missing Controller
Error: Controller could not be found.
Error: Create the class Controller below in file: app/Controller/Controller.php
When I remove ['routeClass' => 'CategoryRoute'], rerouting just works fine.
Have a closer look at the API documenation: API > CakeRoute::parse()
The parse() method is supposed to return an array of parsed parameters (ie $params) on success, or false on failure.

Slug from database available on all pages through lang CodeIgniter

I have a table on my database from where I get some slug texts:
art_and_culture
business_and_financial
auto_and_moto
and display on my website through
<?php echo lang($slug_from_database); ?>
I use this method as LanguageSwticher:
multi-language-support-in-codeigniter
So in My_Controller.php I have in construct:
$this->categories_list = $this->categories_model->entries();
$list_categories['categories'] = $this->categories_list;
$this->data['sidebar_categories'] = $this->load->view('blocks/sidebar_categories', $list_categories, TRUE);
and all the categories are available on all pages from my website. The problem is when I send to the view the categories, the language is not set from the LanguageLoader.php controller witch is initialised in hooks.php (see the link example). If no language no text on echo $slug_from_database. How do you suggest to do?
I found one solution:
Created:
public function LanguageLoader() {
$site_lang = $this->session->userdata('site_lang');
if ($site_lang) {
$this->lang->load('message',$this->session->userdata('site_lang'));
} else {
$this->lang->load('message', $this->config->item('language'));
}
}
in MY_Controller.php
and call it in the constructor: $this->LanguageLoader();
and delete
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);
from hooks.php so now I load the language in controller`s construct not with hook.

How to configure cakephp custom route class

I have create custom router class in cakephp 2.x, I'm just follow this blog post. In my app i don't have /Routing/Route folders and I create folders and put StaticSlugRoute.php file to it. In that file include following code
<?php
App::uses('Event', 'Model');
App::uses('CakeRoute', 'Routing/Route');
App::uses('ClassRegistry', 'Utility');
class StaticSlugRoute extends CakeRoute {
public function parse($url) {
$params = parent::parse($url);
if (empty($params)) {
return false;
}
$this->Event = ClassRegistry::init('Event');
$title = $params['title'];
$event = $this->Event->find('first', array(
'conditions' => array(
'Event.title' => $title,
),
'fields' => array('Event.id'),
'recursive' => -1,
));
if ($event) {
$params['pass'] = array($event['Event']['id']);
return $params;
}
return false;
}
}
?>
I add this code but it didn't seems to working (event/index is working correct).I want to route 'www.example.com/events/event title' url to 'www.example.com/events/index/id'. Is there any thing i missing or i need to import this code to any where. If it is possible to redirect this type of ('www.example.com/event title') url.
Custom route classes should be inside /Lib/Routing/Route rather than /Routing/Route.
You'll then need to import your custom class inside your routes.php file.
App::uses('StaticSlugRoute', 'Lib/Routing/Route');
Router::connect('/events/:slug', array('controller' => 'events', 'action' => 'index'), array('routeClass' => 'StaticSlugRoute'));
This tells CakePhp to use your custom routing class for the URLs that look like /events/:slug (ex: /events/event-title).
Side Note: Don't forget to properly index the appropriate database field to avoid a serious performance hit when the number of rows increases.

Zend_Navigation add class to active link

How can I add a class to the active navigation link? If a link points to URI /index/index and the request URI is also /index/index, I would like the link to have class, for example:
<li class="active">
Index
</li>
This is how I am initializing navigation in the bootstrap:
protected function _initNavigation()
{
$navigation = new Zend_Navigation($this->getOption('navigation'));
$this->view->navigation($navigation);
}
Ok,
I have solved this by writing a controller plugin:
<?php
class My_Controller_Plugin_PrepareNavigation extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer');
$viewRenderer->initView();
$view = $viewRenderer->view;
$container = new Zend_Navigation(Zend_Registry::get('configuration')->navigation);
foreach ($container->getPages() as $page) {
$uri = $page->getHref();
if ($uri === $request->getRequestUri()) {
$page->setClass('active');
}
}
$view->navigation($container);
}
}
This is how to create a navigation() in a layout() with zend frameworks using Application.
Well, at least one way of doing it. the CSS class is set on the
put this into the Bootstrap.php file:
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
include APPLICATION_PATH . '/layouts/scripts/menu.phtml';
$view->navigation($container);
}
This allows you to create an array for a menu in the file menu.phtml, so that you can still maintain the active class on the current link. For some strange reason, if you use this you must include the controller property in the array to get the CSS active class on the current link.
put something like this into the /layouts/scripts/menu.phtml file:
$container = new Zend_Navigation(array(
array(
'label' => 'HOME',
'id' => 'tasks',
'uri'=>'/',
'controller' => 'Index'
),
array(
'label' => 'Contact',
'uri' => 'contact',
'controller' => 'Contact'
),
.... more code here ...
put this into the layout.phtml file:
$options = array('ulClass' => 'menu');

Categories