yii set log path dynamically - php

friends i am using yii log to genrate log file for each user on the base of its id for its actions but when i am trying to set log file path dynamically then its not working please help me to find the way that how i can set dynamically log file path for logged in user.
$logger = new CFileLogRoute();
$logger->setLogPath("/app/protected/runtime/user-log/");
$logger->setLogFile("kumar-121.log");
$message='langusge changed from ';
Yii::log($message, 'CsvError', 'system.*');
buts it not working at all. please help me in this

Thanks friends but i got the answer what is did i firstly created my custom class and extand CFileLogRoute
then i just overwrite some parent file functions
class MyFileLogRoute extends CFileLogRoute{
public function formatLogMessage($message,$level,$category,$time)
{
return #date('Y/m/d H:i:s',$time)." - $message\n";
}
public function getLogFile()
{
return 'user-'.(int)Yii::app()->user->id.'.log';
}
public function setLogFile($value)
{
parent::setLogFile($value . (int)Yii::app()->user->id);
}
and this works perfect for me.
my first method i have created to remove level category from log file so it just simple log file
anyhow thanks

Related

Codeigniter Front End Controller Not working with libraries

I'm posting this after my hair has been ripped out, ran out of rum, and tried everything I can find on google. I've been developing a site using codeigniter which makes use of templates. I've built the backend first and all is working properly there. So now i've started on getting the front end working which is where I'm hitting the issue.
I've created a controller called pages.php which is going to parse the uri string of the current page, use my library to get the page data from the database, then display it. My pages are all created through an editor on the back end and stored in the database.
So here's the pages controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("pages");
}
public function display_page()
{
$page_slug = $this->uri->segment(1);
$data["joes"] = "Here's joes first variable";
$this->pages->get_page($page_slug);
}
}
and here's the error message i get when i hit my url like this demo.mydomain.com/joes-test
and here is how my routes are set up. $route['(:any)'] = 'pages/display_page';
My Pages.php library works perfect on the back end but it's a large file. I've only posted the get_page function below. If you need to see everything let me know. But i dont believe the issue has anything to do with the library itself.
public function get_page($slug){
$objPages = new pages();
$objPages->get_object('slug="'.$slug.'"');
return $objPages;
}
[EDIT] If i place the following inside my homepage controller it works. But the calling function needs to be inside the library.
$this->load->library('pages');
$the_page = $this->pages->get_page("joes-test");
I want to call $this->get_object("joes-test") but this doesn't work. get_object() is an inherited function inside the library.
Now oddly enough. The code i put above will NOT work if i do the exact same thing inside the pages controller
any help leading to a solution would be awesome. I'm under a time crunch and pay to get some assistance. Thanks in advance.
No you can't use the same name with controller and library. please choose another name. for example Mypages for you controller name.
change your routes
$route['(:any)'] = 'mypages/display_page';
then call your controller.
http://demo.mydomain.com/joes-test
I think there nothing wrong with library uri, because as codeigniter official website say: This class is initialized automatically by the system so there is no need to do it manually.
I don't know about lib pages, but how about use
$this->load->view(<file-html>);
and if you want to passing data in variable, you can add variable like this
$this->load->view(<file-html>, $data);
Hope this help, Cheers

How to copy Joomla administrator component as a site one?

I've installed Joomla v3.4.7 to test and prepare my project.I created a component 'HelloWorld' step by step according the official tutorial [https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Using_the_database][1]
,and I succeeded to show the data list,and then the editing page to add or edit existing data, from the Administrator part, just like
localhost/joomla-test/administrator/index.php?option=com_helloworld
After finishing these, I simply copied the files in /Administrator/components/com_helloworld to /components/com_helloworld and overwrite previous files, and access the site component:
localhost/joomla-test/index.php?option=com_helloworld
It didn't work! I used firebug to debug and I got a
NetworkError: 500 Internal Server Error -
http://localhost/joomla-test/index.php?option=com_helloworld
error.... What's happened?
My code:
Site/helloworld.php:
<?php
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');
// Perform the Request task
$controller->execute(JFactory::getApplication()->input->getCmd('task'));
// Redirect if set by the controller
$controller->redirect();
site/controller.php
<?php
// No direct access to this file
defined('_JEXEC') or die;
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* General Controller of HelloWorld component
*/
class HelloWorldController extends JControllerLegacy
{
/**
* display task
*
* #return void
*/
protected $default_view = 'helloworlds';
public function display($cachable = false)
{
parent::display($cachable);
echo "controller";
return $this;
}
}
site/views/helloworlds/view.html.php:
<?php
// No direct access to this file
defined('_JEXEC') or die;
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HelloWorlds View
*/
class HelloWorldViewHelloWorlds extends JViewLegacy
{
/**
* HelloWorlds view display method
* #return void
*/
function display($tpl = null)
{
// Get data from the model
$items = $this->get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;
// Set the toolbar
$this->addToolBar();
// Display the template
parent::display($tpl);
}
/**
* Setting the toolbar
*/
protected function addToolBar()
{
JToolBarHelper::title(JText::_('COM_HELLOWORLD_MANAGER_HELLOWORLDS'));
JToolBarHelper::deleteList('', 'helloworlds.delete');
JToolBarHelper::editList('helloworld.edit');
JToolBarHelper::addNew('helloworld.add');
}
}
Please help, thank you all.
It doesn't work this way (by just copying the folder over). You will have to install the component by packaging it and then installing it on the server. You will need to install the zipped component (that has the XML manifest file) on the server.
Try the following: download the basic HelloWorld component from Joomla, and then install it on your website, and then overwrite it with the files from your localhost.
Site and Administrator have slight differences; the most relevant are related to the template, since in Admin you can count on a standard layout; this is why in an administrator view.html you setup the toolbar and the sidemenu; on the frontend, you create menus pointing to views with configuration.
Your best bet is to create fresh files for controller and view, and then you can create your models inheriting from the administrator modules, that is the best for avoiding code duplication, and it will still leave you the maximum flexibility for customizing the views.
Toolbar cannot work in the front end. It's weird, yes, but if you look it is a separate thing in the administrator folder. It actually checks whether you are in admin as well. I once made a patch to removed the check but it turned out that it would have broken tons of components that had worked around this.
Second, there are many calls to things that rely on relative positioning or that might even explicitly require admin access.
Third, there are indeed some things that are slightly different because in the back end you basically never render the normal view, only the list view and the edit view.
If you want to do admin functions in the front end the best general approach is to look at how com_config, Com_templates and com_modules do it.

Codeigniter how can I make this route work

I've looked around in the documentation and a few other places and I am not sure how to achieve the results I want. I have a domain name set up that will only server public profile pages to visitor. When someone comes straight to the site I want it to display a welcome screen telling more about what the website is about. (example.com) When someone visits example.com/my-public-page I want it to run a default controller that grabs the 'my-public-page from the url, searches the database for a user with that info in a column and displays their info.
I would image that I just set the default_controller to my controller and then check for a second string in the url. If its present search for that in the database. After looking around I am not sure how to make this work and not sure what to search for to get the results I need.
Thanks for your help,
After defining an encryption key for sessions in config.php you can use something like this:
class Wellcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this -> load -> library('session');
if(!$this->session->userdata('visit_time')) {
$this->session->set_userdata('visit_time', time());
redirect('first_visit');
}
}
}

Yii Get MySql query executed

I need get the MySql query executed before Save, Update, Delete for create a personal LOG (audit).
I use $model->save() and $model->delete() standard from CActiveRecord.
Any know how i can do this?
Thanks to all!
You can use the methods
class Objects extends CActiveRecord
{
protected function beforeSave()
{
// Your code goes here
}
protected function beforeDelete()
{
// Your code goes here
}
}
For Logging of query you refer this thread Logging
u can also see the log on the page by just uncommenting the follwing code in config.main file
// uncomment the following to show log messages on web pages
array(
'class'=>'CWebLogRoute',
),

Query caching in CodeIgniter

I want to cache a query in CodeIgniter. What I did for my test is make a controller, that I named show.php:
class Show extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('rejaal_show');
}
public function _remap($method = '',$param = array())
{
$method = intval($method);
$this->output->cache(5);
var_dump ($this->rejaal_show->temp($method));
}
}
And a model that I named rejaal_show.php:
public function temp($id)
{
$this->db->cache_on();
$this->db->where('id',$id);
$query = $this->db->get('system_store_table');
return $query->result();
}
When I call http://localhost/rejaal/show/1 for the first time, it will show a result, but when I call it for the second time, it does not show anything.
I should delete the query cache file to show it again? How should I solve this problem?
With special thanks for your attention.
Can you confirm that you have set $db['default']['cachedir'] to the path of a writable folder in application/config/database.php and that when the query is first run it creates a cache file in there?
The only other reason I can think of for it failing is by your use of the _remap override. I have not used db caching using _remap, but know that CodeIgniter creates a folder called controller+action in your cache folder, and might not be handled very well if using remap? Someone correct me if I am wrong about this.
In the CodeIgniter User Guide page for Web Page Caching, it says:
Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
Do your var_dump inside a view.

Categories