I made a multi-lang app for contents. In the backend, you create content, and show it in a frontend page. I have an issue with the back button (I think its a cache issue) following the next steps:
You click in a content link and access to the content view.
In the current content view (with the current language), you click to
switch language.
It redirects you to home with the new language.
When you press back button, it sends you to the previus content (in
the previus language).
A consideration:
- If I clear the browser cache between the steps 2-3, it works fine.
Here is the code:
In the language switcher controller
$this->lang_array = $this->Language_model->getLangsArray();
if(!isset($this->session->lang)){
if(array_key_exists($this->uri->segment(1), $this->lang_array)){
$this->session->lang = $this->uri->segment(1);
}else{
$this->session->lang = 'es';
}
}
if(isset($this->session->lang)){
if(array_key_exists($this->uri->segment(1), $this->lang_array) && $this->uri->segment(1) != $this->session->lang){
$this->session->lang = $this->uri->segment(1);
}
}
$this->lang->load($this->lang_array[$this->session->lang], $this->lang_array[$this->session->lang]);
To return the view with content, in the contents controller:
$content = $this->db->get_where('contents', array('slug' => $slug), 1)->result();
if(count($content)){
$content = $content[0];
$this->view('content_view', $content));
}else{
return array();
}
Related
I have a small problem on the prestashop on adding custom url into the quick address.
The current status of prestashop is 1.7.4.2 fresh install.
As stated from the image above, I would like to redirect it to external URL http://www.google.com, after done creating it is shown in the quick address menu as shown below:
But when I clicked it, just redirect to:
http://localhost:8080/prestashop_1.7.4.2/admin067c8ousl/index.php/http://www.google.com
Note I have deleted the token as it provided the same result
In other words the token is self generated and differs everytime
I have saw original documentation for that specific issue in here.
When you see on the very bottom, it shows the exact issue I am facing:
Note that you can create links to other websites, for instance your PayPal account or your webmail. Simply paste the complete URL in the "URL" field, including the http:// prefix.
As I have written correct url, but it still thinks it is a controller.
I have no modified any code yet, is there a way to fix it.
Thank You and Have a nice day.
That was for v1.6, v1.7 doesn't allow external urls by default. I submitted an improvement for this, hope they approve the merge. Meanwhile, if you want to use them you can modify the classes/QuickAccess.php or add to the override (better option) and change the function getQuickAccessesWithToken to the following:
public static function getQuickAccessesWithToken($idLang, $idEmployee)
{
$quickAccess = self::getQuickAccesses($idLang);
if (empty($quickAccess)) {
return false;
}
$baselink = Context::getContext()->link->getBaseLink();
foreach ($quickAccess as $index => $quick) {
if(strpos($quickAccess[$index]['link'], 'http') !== 0 or strpos($quickAccess[$index]['link'], $baselink) === 0){
if ('../' === $quick['link'] && Shop::getContext() == Shop::CONTEXT_SHOP) {
$url = Context::getContext()->shop->getBaseURL();
if (!$url) {
unset($quickAccess[$index]);
continue;
}
$quickAccess[$index]['link'] = $url;
} else{
// first, clean url to have a real quickLink
$quick['link'] = Context::getContext()->link->getQuickLink($quick['link']);
$tokenString = $idEmployee;
preg_match('/controller=(.+)(&.+)?$/', $quick['link'], $admin_tab);
if (isset($admin_tab[1])) {
if (strpos($admin_tab[1], '&')) {
$admin_tab[1] = substr($admin_tab[1], 0, strpos($admin_tab[1], '&'));
}
$quick_access[$index]['target'] = $admin_tab[1];
$tokenString = $admin_tab[1].(int)Tab::getIdFromClassName($admin_tab[1]).$idEmployee;
}
$quickAccess[$index]['link'] = $baselink.basename(_PS_ADMIN_DIR_).'/'.$quick['link'];
if (false === strpos($quickAccess[$index]['link'], 'token')) {
$separator = strpos($quickAccess[$index]['link'], '?') ? '&' : '?';
$quickAccess[$index]['link'] .= $separator.'token='.Tools::getAdminToken($tokenString);
}
}
}
}
return $quickAccess;
}
Override is not a clean solution.
You can use free module to adding jquery to your "admin header hook" and do it by jquery to change URL of new created quickAccess
I am trying to redirect to a page on my external page, what I have tried till now is. Manually I added some files.
In /custom/modules/Users/logic_hooks.php
<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['after_logout'] = Array();
$hook_array['after_logout'][] = Array(
//Processing index. For sorting the array.
1,
//Label. A string value to identify the hook.
'after_logout example',
//The PHP file where your class is located.
'custom/modules/Users/logic_hooks_class.php',
//The class the method is in.
'logic_hooks_class',
//The method to call.
'after_logout_method'
);
?>
And another file in In /custom/modules/Users/logic_hooks_class.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class logic_hooks_class
{
function after_logout_method($bean, $event, $arguments)
{
header('Location: http://raviranjan.info/');
}
function AfterLogout(&$bean, $event, $arguments)
{
SugarApplication::redirect('http://raviranjan.info/');
}
}
?>
So is there any other way to redirecting or just show something on screen before or after logging out from SugarCRM application.
Advance Thanks for any help.
There are multiple ways of doing this. Read following details:
show alert of any other type of message by handling click event of logout link. e.g. when user click that link then show alert of any other type of message.
after logout redirect user to your custom page( you can build custom entry point which will be accessible with auth=>false).
you can add any button or java-script to show message then redirect it to your target page.
Simple jquery selector, see following:
$("a.utilsLink").click(function(){
var r = confirm("Are you sure to logout?");
if (r == true) {
console.log("yes is clicked");
} else {
console.log("cancel is clicked...");
return false;
}
});
I made one of my menu items only for Guests (not logged in users). ANd If user logs in, it disappears from menu. It works fine...
But I noticed that when I put the link to the Guest menu item in browser, when I'm lnot ogged in , it shows very disgusting jos-Error: You are not authorised to view this resource.
I want to find a way to track such errors and redirect to custom 404 page or some other page.
Please, assist me in this issue.
Thanks
The string You are not authorised to view this resource located in JERROR_ALERTNOAUTHOR language string and fire in includes/application.php function:
public function authorise($itemid)
{
$menus = $this->getMenu();
$user = JFactory::getUser();
if (!$menus->authorise($itemid))
{
if ($user->get('id') == 0)
{
// Redirect to login
$uri = JFactory::getURI();
$return = (string)$uri;
$this->setUserState('users.login.form.data', array( 'return' => $return ) );
$url = 'index.php?option=com_users&view=login';
$url = JRoute::_($url, false);
$this->redirect($url, JText::_('JGLOBAL_YOU_MUST_LOGIN_FIRST'));
}
else {
JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR'));
}
}
}
You could alter this function and add redirection or change the text string with your desired message.
Redirect could be done with redirect() method.
An example of redirect syntax:
$app = JFactory::getApplication();
$app->redirect(JRoute::_(JURI::root().'index.php'));
Please make sure that you won't alter application.php without creating an override system plugin.
Hope this helps
I have some experience with php, but I just recently started learning Codeigniter. I used to have a website with a fixed navigation pane and sidebar, but the main section of the site loaded dynamically based on a Get variable. It was basically
include head.php
include navbar.php
include sidebar.php
include the page requested from the get variable (home, about, contact, etc.)
include footer.php
I liked this because the entire site did not have to reload when the user navigated from page to page.
I can't figure out how do this with Codeiginiter. Should I be using a controller for each page or one controller with a function for each page? Do anyone know of a good tutorial that does something similar? All the tutorials I've seen reload the entire site for every page.
Edit: Essentially I want to do this but with Codeigniter
Since it looks like you want relatively static content, but loaded dynamically you can do with one controller and (maybe) one method in the controller.
To do it with one method, do this in the welcome controller:
public page( $page_id ){
// views/header.php
$this->load->view( "header" );
if( $page_id = "about" ){
$this->load->view("about"); // views/about.php
}
else if( $page_id = "contact" ){
$this->load->view("contact"); // views/contact.php
}
// views/footer.php
$this->load->view("footer");
}
This takes a single get variable and figures out what page to load in between the header and footer.
This way www.yoursite.com/page/about will load the about page, www.yoursite.com/page/contact will load the contact page
Now, if you want to get rid of the /page part, you need to do some URL rerouting in application/config/routes.php
Alternatively you could use several methods in one controller:
public about( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "about" );
// views/footer.php
$this->load->view("footer");
}
public contact( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "contact" );
// views/footer.php
$this->load->view("footer");
}
Now your URLs look nicer without routing, but you have to load the header/footer for every page.
do you really like to copy/paste many $this->load->view() to any controller function?
It's a spaghetti code. You can try next: for example we have main.php controller as default controller. This main controller contain main function:
public function index()
{
ob_start();
$this->load->model('mainmodel');
$data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
foreach ($data->result_array() as $row) {
$this->load->module($row['block_name']);
$this->name = new $row['block_name'];
$this->name->index();
}
ob_end_flush();
}
So, each other controller also have index() function which can dispatch actions depends on url segments, prepare params etc.
Footer controller as example (I use Smarty as template engine):
public function index()
{
$this->mysmarty->assign('year', date("Y"));
$this->mysmarty->view('footer');
return true;
}
Content controller will have:
public function index()
{
$name = $this->uri->segment(1, 'index');
$act = $this->uri->segment(2, 'index');
$this->load->module($name);
$this->name = new $name;
$pageData = $this->name->_show($act);
if ($pageData)
{
$this->mysmarty->assign($name, $pageData);
}
$this->mysmarty->view($name);
}
Thats mean what if you want to show http://site.name/page/contactus , we do next:
1) main.php start cycle by needed blocks
2) firstly we show header.tpl by header controller
3) then we show menu
4) then we call content controller which parse url, found what he should call _show() function in Page controller and pass action='contactus' to it. _show() function can contain some switch/case construction which show templates depends of action name (contactus.tpl in this case)
5) in the end we show footer template
In such case we have flexible structure. All controllers should have index() functions and all controllers who can be called in content should have _show($act) function. Thats all.
In codeIgniter you can do that like this, you can load different views at the same time from your controller. for example:
for example in your navbar view you have a Contacts button in your menu that would look like this:
<a href='contacts'>Contacts</a>
In your controller:
public function contacts()
{
$this->load->view('header');
$this->load->view('navbar');
$this->load->view('sidebar');
$this->load->view('contacts_view');
$this->load->view('footer');
}
So we're assuming here that you have the following views already that is ready to be loaded (header.php, navbar.php, sidebar.php, contacts_view.php, footer.php).
UPDATE:
you don't need to have $_GET[] request, just provide the method name from your controller in the <a> anchor tag
in codeigniter i using template
first make template file in one folder with header.php, navbar.php, etc.
example : template.php
<?php
echo $this->load->view('header'); //load header
echo $this->load->view('navbar');//load navbar
echo $this->load->view('sidebar');//load sidebar
echo $this->load->view($body); //load dynamic content
echo $this->load->view('footer');//load footer
?>
second in controller
function index( ){
$data['body'] = 'home'; // cal your content
$this->load->view('template', $data);
}
My home page url looks like this
http://localhost/mediabox/home/box/12
I have a link of language on home page when a user clicks on that link i am sending that language id as query string , the page reloads and the url converts to
http://localhost/mediabox/home/box/?bid=12&ln=2
I want to reload the page with the new language but don't want to change my url i-e I want my URL to be
http://localhost/mediabox/home/box/12
after the page loads
How it is possible please me some gud ideas
Thanks
VIEW
<a href=<?php echo site_url('home?language=indonesian');?>>Indonesian language</a>
CONTROLLER
class Home extends CI_Controller {
public function index()
{
$language = $this->input->get('language');
if($language){
// Put your code here
// Now u can set session
$this->session->set_userdata('language', $language);
redirect('home');
}
if($this->session->userdata('language'))
{
var_dump($this->session->userdata('language'));
}
echo 'Hello World!';
}
}