I'm using Codeigniter + Grocery Crud.
So, GC working good but is blank.. shows results from database, but search option and css style doesn't load.
My structure is:
application
assets
system
GC is inside assets.
My controller load view and send output to view:
public function output($output = null) {
$this->load->view('welcome_message', $output);
}
public function users() {
$this->load->library('grocery_CRUD');
$this->output((object) array('output' => '', 'js_files' => array(), 'css_files' => array()));
try {
$crud = new grocery_CRUD();
$crud->set_theme('flexigrid');
$crud->set_table('suppliers');
$output = $crud->render();
$this->output($output);
} catch (Exception $e) {
show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
}
}
After that, i'm using CG official tutorial to include jQuery and CSS files in the view. They are there. When i click over (in view source) - i can see them. It's not problem in folders.
I saw in View source double html tags... Is it exist any codeigniter autoload library to do that?
I'm defined autoload helpers - url and utility, which i made for loading assets folder.
I'm supposing the problem is ajax, jquery...but how to fix it?
Grocery CRUD output contains an array for adding java-script files
If you want to add extra/custom js file then you have to do something like this:
$output = $crud->render();
array_push($output->js_files, base_url("assets/my_js_folder/myjs.file.js"));
array_push($output->js_files, base_url("assets/another_js/config.module.js"));
$this->output($output);
I really hope this help you :)
Related
I'm brand new to CodeIgniter, so apologies if I'm missing something obvious here.
I'm comfortable with sending data from a controller to a view file using return view('default/blog/index', $data);. My issue is accessing the same data in a layout file which is extended by the view file using <?= $this->extend('layouts/default'); ?>.
For example, if I insert <?= $data['content'] ?> in my view file, it displays as expected. If I insert the same code in the layout file that is extended by my view file, I get the "Trying to access array offset on value of type null" exception.
What am I missing that will allow me to access my data from within the layout file?
Thanks in advance.
Update:
So in my BlogController I've got
class BlogController extends BaseController
{
public function index()
{
$model = new Blog();
$data = $model->getBlog();
return view('default/blog/index', ['data' => $data]);
}
public function item($slug = null){
$model = new Blog();
$data = $model->getBlog($slug);
return view('default/blog/item', ['data' => $data]);
}
}
And then in my item.php and index.php files, I have
<?= $this->extend('layouts/default', ['data' => $data]); ?>
My Blog Model's getBlog() method:
public function getBlog($slug = false)
{
if ($slug === false) {
return $this->orderBy('bs_created_dt', 'desc')->findAll();
}
return $this->where(['bs_slug' => $slug])->first();
}
When I use the debug toolbar to inspect the data, it is showing as expected, and I can display it in the view files, but not in the layout file.
In Codeigniter, you need to pass data also in an extended file called layouts.
Because you want to access data inside the extended file and for that, you just need to pass data to that file.
So replace the line of code of extended view with this :
$this->extend('layouts/default', ['data' => $data]);
Figured this out - absolute rookie mistake.
I'm working off of a pre-existing template, and the previous developer had overwritten the $data variable in layout file before where I was trying to use it.
I'm off to stand in the corner for a while.
I have a project where people can book places on events. There are dozens of events. I have been requested to generate a report listing attendees so they can be checked in at the door. Because there are so many events, I would like to add an "Attendees Report" button to the editform for each event (inside the CMS). I have created the button, and it works.
BUT... instead of getting the data in a CSV, I get the (correct) data displaying in the CMS, as if it was being echoed.
Here is my function
public function getCustomersForEvent($data, $form){
$attendees = CustomerOrderLine::get()->filter(array("EventID" => $data["ID"]));
if(!$attendees){
return false;
}
$fileName = "report.csv";
$separator = ",";
$csvColumns = array("ID", "Description", "Customer");
$fileData = "";
foreach($attendees as $row){
$fileData .= $row->Event()->EventName . $separator;
$fileData .= $row->CustomerOrder()->Customer()->FirstName . " " . $row->CustomerOrder()->Customer()->Surname . "\n";
}
return SS_HTTPRequest::send_file($fileData, $fileName, 'text/csv');
}
I have had a look at this http://www.silverstripe.org/community/forums/general-questions/show/15325 but the data echoed to the CMS anyway.
Can someone please tell me what I am doing wrong? As I said, the data is fine, but I'm not getting the CSV Open or Save dialog.
The solution will be in how you are calling this. I would advise that you make a complete new controller or modify a module like...
https://github.com/axyr/silverstripe-phpexcel
...where you can copy the php code and modify it to return just the data you require from above.
To make a controller that is only focused on downloading a file...
class CustomersForEvent_Controller extends Page_Controller {
private static $allowed_actions = array (
'downloadcsv',
);
public function downloadcsv() {
return SS_HTTPRequest::send_file("my content",'myfile.csv','text/csv');
}
}
and add that controller to the routes in _config/config.yml...
Director:
rules:
'attendees': 'CustomersForEvent_Controller'
...and then you can just link to yousite.com/attendees/downloadcsv
This has been tested locally and confirmed to work
Is this report to be generated from within the CMS itself? If so, do you have a ModelAdmin for managing CustomerOrderLine objects? If so, can you not use the GridField's "Export to CSV" button?
In order to output anything more than just those fields displayed by the GridField itself, you'll need to create/alter your model class's $getExportFields() method to return an array of all the fields on your model you wish to be exported in this manner.
So after fixing mustache and Apache perms I now have ran into an issue where the site will load the index page, but any page after that located in (the same place a index.html) /pages as it fails to load.
What you can see below is the index.php for Slim to do it's stuff. I can't really understand why the index page will load just fine but no other page will load. If you can point me in the right direction then it would be greatly appreciated.
Examples:
End User > myexample.com/
Location > myexample.com/pages/index.html
The above works just fine but if you look below, you should be able to understand my issue.
End User > myexample.com/info
Location > myexample.com/pages/info.html
The End User is what you see on the site, but the location is where the files belong.
Thanks in advance, Luke.
<?php
// Load up composer autoload, and instantiate the application.
require 'vendor/autoload.php';
$app = new \Slim\Slim;
// Register a singleton of the Mustache engine, and tell it to cache
$app->container->singleton('mustache', function () {
return new Mustache_Engine(array(
'cache' => 'storage',
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__) . '/pages', array('extension' => '.html'))
));
});
// Helper function to render templates
function renderTemplate($name, $data = array()) {
global $app;
if (file_exists('config.php')) {
$data = (require 'config.php');
}
$data += array(
'resourceUri' => $app->request->getResourceUri() ?: 'index',
'request' => $app->request
);
return $app->mustache->loadTemplate($name)->render($data);
}
// Loads a page by the given name/path
function loadPage($path) {
global $app;
// Set up the base path
$f = 'pages/' . $path;
if (file_exists($f . '.html')) {
// If there's an HTML file, render the mustache template
return renderTemplate($path . '.html');
} elseif (file_exists($f . '.php')) {
// If there's a PHP file, return it
return (require $f . '.php');
} elseif ($path != '404') {
// Otherwise, go get the 404 page
return loadPage('404');
} else {
// But if the user doesn't have a 404 page made, return a plain 404
$app->halt(404);
}
}
// Index page
$app->get('/', function () use ($app) {
echo loadPage('index');
});
// Route to everything else
$app->get('/.*?', function () use ($app) {
// Get the current request path
$path = $app->request->getResourceUri();
// Make sure the user isn't trying to escape and do nasty things
if (!preg_match('/^[A-z\/\-\_]+$/', $path)) {
echo loadPage('404');
}
// Send the page off to get loaded
echo loadPage($path);
});
$app->run();
I think you'll find your problem isn't with Slim but rather with the custom functions you wrote to allow Slim to load Mustache templates.
I would highly recommend removing those functions and using the custom Slim Mustache view provided in the views directory of Slim-Extras repo. At that point, any issues with Slim can be more easily diagnosed as there won't be custom functions to debug.
NOTE: While the Slim-Extras repo is deprecated in favor of the Slim-Views repo, the custom Mustache view (which hasn't made it to the Slim-Views repo) should work fine.
For reference please see the Custom Views section of the Slim documentation.
UPDATE: I've been using Dearon's Slim Mustache library for integration with Slim View in a new app of mine and Justin Hileman's PHP implementation of Mustache. Both are highly recommended and simple to install and configure. Good luck!
I'm trying to integrate custom code into my Laravel project, here's the current folder structure :
You can see my controllers and models - views are in a module subfolder and I already made the controller and views run properly, but now I want to load a config file under module/Csol/Fight/Config, named development.php (for example) - here's my current attempt :
In my controller :
public function __construct()
{
$namespacePath = substr(__NAMESPACE__, 0, strrpos(__NAMESPACE__, "\\"));
View::addNamespace($namespacePath, app_path()."/module/".str_replace("\\", "/", $namespacePath).'/views');
Config::addNamespace($namespacePath, app_path()."/module/".str_replace("\\", "/", $namespacePath).'/Config');
var_dump(get_class(Config::getFacadeRoot()));
}
public function showWelcome()
{
// View::addLocation(app_path().'/module/Csol/Fight/views');
echo "<pre>";
var_dump($this);
echo "</pre>";
var_dump(Csol\Fight\Config::getItems());
die();
return View::Make("Csol\Fight::a");
}
development.php:
return array(
"dbs"=>array(
"host"=>"sss",
"db_name"=>"ccc",
)
);
I can't get the result of my own config file, any help please ?
Problem fixed...
takes me a little more time...
I should use this var_dump(Config::get('Csol\Fight::development.xxx'));
instead of var_dump(Csol\Fight\Config::getItems());
have fun...
dir:
application
-controllers
-models
-views
-mobile_views
How do I auto load templates at mobile_views when I use $this->load->view and view by iphone or other mobile phone?
Check this
You can do it in two way.
Way 1: Its very simple. In the above answer (the link I have given) add following line in the end of MyController function
$this->load->_ci_view_path . = $this->view_type .'/';
You are done. You can simply load view like normal view load.
Way 2:
To autoload a view based on user agent, I think you can implement it using hooks. To implement this hooks you need to follow the following steps
Autoload user agent library in autoload.php
$autoload['libraries'] = array('user_agent');
Enable hooks in config.php
$config['enable_hooks'] = TRUE;
Not implement hooks on post_controller_constructor. Add following codes to hooks.php
$hook['post_controller_constructor'][] = array('class' => 'Loadview',
'function' => 'load',
'filename' => 'loadview.php',
'filepath' => 'hooks'
);
Now create a page named loadview.php under hooks directory having following code
class Loadview
{
public static $MOBILE_PLATFORM = 'mobile';
public static $DEFAULT_PLATFORM = 'default';
public function load(){
$this->CI =& get_instance();
$view_type = $this->CI->agent->is_mobile() ? self::$MOBILE_PLATFORM : self::$DEFAULT_PLATFORM;
$this->CI->load->_ci_view_path = $this->CI->load->_ci_view_path . $view_type .'/';
}
}
You are done now. You can simply load view like normal view load.
to load views from another dir aside from "views", i found this forum topic to be helpful
http://codeigniter.com/forums/viewthread/132960/
function external_view($path, $view, $vars = array(), $return = FALSE)
{
$full_path = $path.$view.'.php';
if (file_exists($full_path))
{
return $this->_ci_load(array('_ci_path' => $full_path, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
else
{
show_error('Unable to load the requested module template file: '.$view);
}
}
and you can work the rest from the controller.
I do this in my controller:
public function index()
{
if($this->agent->is_mobile())
{
$this->load_mobile();
}
else
{
$this->load_web();
}
}
public function load_mobile()
{
$this->load->view('mobile/home');
}
public function load_web()
{
$this->load->view('web/home');
}
In this way I can add different data to mobile and to web pages.
I also extend the default controller and add some useful extra features:
Enables the usage of master page/templates.
Can add css and javascript files.
Uses the _output method for controlling the controllers output.
Can load relative content with in the form of modules (views)
So I can manage better the different pages.
Bye!!