I am trying to make a CRUD Module in Codeigniter HMVC but I seem to be missing something in the process. Here is what I am facing.
I have a News Module which has a Manage function
function manage(){
$grid = Modules::run('Crud/renderGrid', 'News' , 'News management');
}
Render Grid Function
function renderGrid($module , $page_title){
$data['page_title'] = $page_title; //Dynamic
$data['module'] = $module;
$data['view_module'] = 'Crud';
$data['displayfields'] = Modules::run($module.'/get_displayfields');
$data['key'] = Modules::run($module.'/get_key');
$data['rows'] = Modules::run($module.'/get' , $data['key']);
$data['view_file'] = 'manage';
$this->load->module('dashboard');
$this->dashboard->show_dashboard($data);
}
Here, the show_dashboard function just loads up a template layout with a desired view in it.
function show_dashboard($data = NULL){
if($data == NULL){
$data['view_file'] = "manage";
$data['page_title'] = 'Sigma Web Solutions';
}
$this->load->module('templates');
$this->templates->admin($data);
}
Templates->admin
function admin($data){
$this->load->view('admin' , $data);
}
The View (omitting the header n Footer)
<?php
if (!isset($view_file)) {
$view_file = "";
}
if (!isset($view_module)) {
$module = $this->uri->segment(1);
}
if (($view_module!="") && ($view_file!="")) {
$path = $view_module."/".$view_file;
$this->load->view($path);
}
?>
Now, when I try the url news/manage, it gives me a blank page with no source code in it. But when I try something like
crud/renderGrid/news/sometitle/ it works just fine.
Kindly point out what did I miss here. Thanks.
Working Solution:
Thanks to wolf I added a route
$route['managenews']= 'crud/renderGrid/news/News';
And it works like charm. But why do I need a route here? Shouldn't it just work. And this means for every module I need to have 4 entries in my route file for the CRUD system to work. can anyone suggest a better method?
Related
here I am again, had before more complicated issue, but now got another (smaller I think).
In template file there's variables in use as "author_id", "authlink", but in controller file doesn't getting author url (but it should).
In template file there's defined variables as authlink and author_id.
Also, in controller file I've included file path where is these variables in use.
<?php
set_include_path(".:/domains/example.com/public_html/.../single-2.php"); //destination to file where's author_id variables either.
function ajax_rental_add_to_cart()
{
if ( STInput::request( 'action' ) == 'rental_add_cart' ) {
$response = array();
$response['status'] = 0;
$response['message'] = "";
$response['redirect'] = '';
$author_id = get_post_field( 'post_author', get_the_ID() );
$authlink = get_author_posts_url($author_id);
if ( $this->do_add_to_cart() ) {
$response['redirect'] = $authlink;
$response['status'] = 1;
wp_send_json($response, 200);
} else {
$message = STTemplate::message();
$response['message'] = $message;
wp_send_json($response, 200);
}
}
}
static function inst()
{
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
}
STRental::inst()->init();
}
?>
Not sure is that static function inst() related, but pasting anyway. Problem is, on variable $response['redirect'] = $authlink; I should be redirected to https://example.com/author/author_id , but I'm getting redirected only into https://example.com/author/
It seems like controller file doesn't getting anyway author_id, maybe because after redirect it loosing author_id and that's why I'm not redirected to right way? Should I store author_id somehow and use it again in this file?
P.S. If I'm using same redirection in template file (not controller) - works perfectly, but then bypassing all date checks, so that's why I'm still messing up with controller file.
I am getting error page not found when I implementation URL using slug.
This is code in controller
function view($slug)
{
$this->data['halaman_item'] = $this->mhalaman->get_profil($slug);
if (empty($this->data['halaman_item'])) {
show_404();
}
$this->data['title'] = $this->data['halaman_item']['judul']; $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('mahasiswa/profil/sejarah', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_content',$this->data);
}
Model
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
Route
$route['profil/view'] = "profil/view";
$route['profil/(:any)'] = "profil/view/$1";
$route['profil'] = "profil";
When I run this link tkd/index.php/mahasiswa/profil/sejarah is 404 page is not found. may be you know where is the probelm.
Help me what to do. Thank you.
Well, without knowing how much of the rest of your app works...the first obvious thing is making sure $this->data['halaman_item'] actually has data in it...because if not, it's calling show_404 before anything happens.
I'm working on codeigniter and I wonder whats the best way to change title dynamically. Eg. title will change depending if you are on home page, single post page, category pages, etc.
The only solution i can think of is to make separate function and compare current URL ( from address bar ) with structure of the single post page, category page, home page
Something like this:
public function current_title() {
if($this->uri->segment(2) == 'post') {
// will return post title
}
if($this->uri->segment(2) == 'category') {
// will return archive title
}
if(current_url() == base_url()) {
// this is home page
}
If anyone worked with this before, any advice highly appreciated
I would not use the uri for this, but instead the controller and action name and the language class :
public function current_title()
{
$this->lang->load('titles.php', 'en');
return $this->lang->line(
$this->router->fetch_class().'.'.$this->router->fetch_method()
);
}
You will have a key like MyClass.myMethod for your translation. Just add your titles in your titles.php file :
$lang['MyClass.myMethod'] = "The title";
$lang['MyOtherClass.myOtherMethod'] = "The other title";
Read more about translation :
http://ellislab.com/codeigniter/user-guide/libraries/language.html
http://ellislab.com/codeigniter/user-guide/helpers/language_helper.html
//in the controller you should do like this:
class Home extends your_Controller {
public function __construct() {
parent:: __construct();
}
function index()
{
$this->data['pageTitle'] = 'Your page title';
$data['main_content'] = 'home';
$this->load->view('includefolder/viewname', $data);
}
}
This is how I do it:
$PHPFile = basename($_SERVER['PHP_SELF'],'.php');
switch ($PHPFile) {
case 'index': $PageTitle = 'Home'; break;
case 'products': $PageTitle = 'Products'; break;
case 'services': $PageTitle = 'Services'; break;
}
You can use string searches or whatever is needed. I use this method since I have the header of the page as a function in library.
As we have a controller function for each view so you can easily get function name from url
$this -> router -> fetch_module();
so you can work with it.
I have a controller called user which just loads the user profile page for now
class user extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index($username = null){
//load index page
$this->load->view('profile/index');
}
}
i have also routed it so i can load it from user/$username in routes
//user profiles pretty url
$route['user/(:any)'] = "user/index/$1";
the thing is i would like to change it and allow directly the users to go to their profiles without typing user/$username and instead $usernamd like mysite.com/$username...
I tried it but it messes up everything.how can i achieve this?
Thanks.
I guess the only way to achieve something like this is to add all other controllers to your routes file.
You could try something like this
$route['controller'] = "controller";
$route['controller/(:any)'] = "controller/$1";
$route['(:any)'] = "user/$1";
Combined with the _remap function as stated here. In your users controller.
Have you heard of the _remap function?
If you replace the index() function with this:
public function _remap($username = null) {
$this->load->view('profile/index');
}
It will probably work. You don't have to use the routes.php.
I used something like this for my users ; this "p" function in my users controller, mysite.com/users/p/$user_id , routes are good but I solved it like this, you could also do it do index function if you don't want something like "p"
function p()
{
$total_slashes = count ( $this->uri->segment_array () );
$last = end ( $this->uri->segments );
if ($total_slashes == 3) {
$data ['userdetails'] = $this->users_model->userDetails ( $last );
// $last is our user_id
$this->load->view('profile/index');
}
}
I am using Kohana 3.2 and I am having problems calling the ouput of a controller in another controller.
What I want...
In some pages I have got a menu, and in others I don't. I want to use make use of the flexability of the HMVC request system. In the controller of a page I want to call another controller which is responsible for the creation of the menu.
What I have a the moment:
file menu.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Menu extends Controller
{
private $_model = null;
public function __construct(Request $request, Response $response)
{
parent::__construct($request, $response);
$this->_model = Model::factory('menu');
}
public function action_getMenu()
{
$content = array();
$content['menuItems'] = $this->_model->getMenuItems();
// Render and output.
$this->request->response = View::factory('blocks/menu', $content);
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
}
}
somepage.php
public function action_index()
{
$this->template->title = 'someTitle';;
$contentData['pageTitle'] = 'someTitle';
$contentData['contentData'] = 'someData';
#include the menu
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
$this->template->content = View::factory('pages/somePage', $contentData);
$view = $this->response->body($this->template);
$this->response->body($view);
}
If I uncomment the following line in menu.php, I see the menu rendered:
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
So I guess that part is alright. The problem is in the following line in somepage.php:
$menuBlock = Request::factory('menu/getMenu')->execute();
This gives me back a response object. Whatever I do, I do not get the output in $this->template->menu.
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
What must I do to have $this->template->menu contain the view, so I can use it correctly?
I hope this all makes sense. This is the way I would like to do it, but maybe I am completely on the wrong track.
I would do it this way:
class Controller_Menu extends Controller
{
public function action_build()
{
// Load the menu view.
$view = View::factory('navigation/menu');
// Return view as response-
$this->response->body($view->render());
}
}
In your controller get the menu as follows:
// Make request and get response body.
$menu = Request::factory('menu/build')->execute()->body();
// e.g. assign menu to template sidebar.
$this->template->sidebar = Request:.factory('menu/build')->execute()->body();
I would not use the __construct method in your controllers. Use before() instead, this is sufficient for most of the problems (for example auth):
public function before()
{
// Call aprent before, must be done here.
parent::before();
// e.g. heck whether user is logged in.
if ( !Auth::instance()->logged_in() )
{
//Redirect if not logged in or something like this.
}
}
I found the answer to my problem in less than an hour after asking.
I just forgot to put it here.
In somePage.php change :
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
To:
$this->template->menu = Request::factory('menu/getMenuBlock')->execute()->body();
And in menu.php change:
$this->request->response = View::factory('blocks/menu', $content);
To:
$request = View::factory('blocks/menu', $content);
$this->response->body($request);
I hope this will help someone else.