How use function in Parse Library code igniter? - codeigniter

I use a parser library in CodeIgniter for templates.
Assume i have the below html code
<html><body><p>Hello {name}</p></body></html>
and my Controller is:
$data['name'] = 'Jason';
$this->parser->parse('view',$data);
now I want to use the function in the html code below:
<html><body><p>Hello **strtolower({name})**</p></body></html>
Is it possible to use a function in the parse tag?

You can use this code :
public function home_page() {
$theme = "<html><body><p>Hello".strtolower("'{name}'")."</p></body></html>";
$frame = $this->parser->parse($theme, array('name'=>'DENY'), TRUE);
$this->parser->parse("index-page.html", $frame);
}

This can't be done out of the box, that I know of. You'll have to extend the Parser Library. I did a quick Google search and found this. It seems to be what you're looking for, although it is 4 years old..
https://gist.github.com/svizion/3480575
Hope this helps.

Related

how to integrate twilio php video with codeigniter

I am trying use twilio video in a codeigniter.
(1) in config.php added this
$config['composer_autoload'] = 'Twilio/autoload.php';
(2) added Twilio directory where my application folder is.
folder structure
application
system
Twilio
In My controller code
public function startRoom()
{
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Jwt\Grants\SyncGrant;
use Twilio\Jwt\Grants\IPMessagingGrant;
$TWILIO_ACCOUNT_SID = '';
$TWILIO_API_KEY = '';
$TWILIO_API_SECRET = '';
}
I am getting this error
Parse error: syntax error, unexpected 'use' (T_USE) in D:\xampp\htdocs\video_code\application\controllers\Welcome.php on line 15
Line 15 is - use Twilio\Jwt\AccessToken;
Twilio developer evangelist here.
Tpojka had the right answer, I just wanted to follow up so that other people that might see this can see the answer.
The key is that, as the documentation says:
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.
So, you want to update your controller code to look like this:
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Jwt\Grants\SyncGrant;
use Twilio\Jwt\Grants\IPMessagingGrant;
public function startRoom()
{
$TWILIO_ACCOUNT_SID = '';
$TWILIO_API_KEY = '';
$TWILIO_API_SECRET = '';
// and so on...
}
Hope this helps.

Code igniter url segment

currently my website url is
https://www.thefoolsbookie.com/main/inside?id=8
but I want this to be like this
https://www.thefoolsbookie.com/nfl
How can I do this?
Edit the application/config/routes.php file and add a new route for it
$route['nfl'] = 'main/inside';
It should be as simple as that :)
See https://ellislab.com/codeigniter/user-guide/general/routing.html for more examples.
Edit
I've worked with CI for a long time and I've never seen it use GET params in the routes file in that way. I'm not sure it is possible.
You could have it like $route['nfl'] = 'main/inside/8';
then in the main controller your inside method would look like:
public function inside($id)
{
//$id would contain the ID of 8 when you go to
//https://www.thefoolsbookie.com/main/inside/8
//or https://www.thefoolsbookie.com/nfl
}

breadcrumbs in codeigniter (MVC)

I want some help with breadcrumb in codeigniter. It's my first experience with CodeIgniter and I don't have to clear enough how can I declare this function at Controller
public function breadcrumb(){
$this->load->library('breadcrumb');
function Tutorial() {
parent::Controller();
$this->load->library('breadcrumbcomponent');
$this->breadcrumbcomponent->add('Home', base_url());
$this->breadcrumbcomponent->add('Tutorials', base_url().'tutorials');
$this->breadcrumbcomponent->add('Spring Tutorial', base_url().'tutorials/spring-tutorials');
}
}
in View. I create the library as it is showing here:
http://www.technicalkeeda.com/codeigniter-tutorials/how-to-create-bread-crumb-using-php-codeigniter
Help me to understand better and to solve this case!
Best :)
Please edit your question and include proper code tags where needed. I will assume that you are using new version of CodeIgniter, but old tutorial (one that is related to CI version less than 2). Try to change this in code should be working that way.
In controller, change this code:
function Tutorial(){
parent::Controller();
$this->load->library('breadcrumbcomponent');
}
with this one:
public function __construct()
{
parent::__construct();
$this->load->library('breadcrumbcomponent');
}
Library class could stay the same, I'd say.

CodeIgniter - Dynamic URL segments

I was wondering if someone could help me out.
Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.
As per the CI userguide the uri is built as follows
www.application.com/CLASS/METHOD/ARGUMENTS
This is fine except i need to structure that part a bit different.
In my forum i have categories and posts, so to view a category the following url is used
www.application.com/forums
This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be
www.application.com/forums/mycategory/this-is-my-first-post
I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.
If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.
Cheers,
Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction
First Post
In the controller you can easily get this
$category = $this->uri->segment(3);
$post = $this->uri->segment(4);
And now you can proceed.
If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.
Go to system/core/uri.php and add this method
function assing_segment($n,$num)
{
$this->segments[$n] = $num;
return $this->segments[$n];
}
How to use
$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');
And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
You could make a route that forwards to a lookup function.
For example in your routes.php add a line something like;
$route['product/(:any)/(:any)'] = "forums/cat_lookup/$1/$2";
This function would then do a database lookup to find the category.
...
public function cat_lookup($cat, $post) {
$catid = $this->forum_model->get_by_name($cat);
if ($catid == FALSE) {
redirect('/home');
}
$post_id = $this->post_model->get_by_name($post);
/* whatever else you want */
// then call the function you want or load the view
$this->load->view('show_post');
}
...
This method will keep the url looking as you want and handle any problems if the category does not exist.Don't forget you can store the category/posts in your database using underscores and use the uri_title() function to make them pretty,
Set in within config/routes.php
$route['song-album/(:any)/:num'] = 'Home/song_album/$id';
fetch in function with help of uri segment.
$this->uri->segment(1);

Beginners Tutorial for Zend Framework implementing model and call it from Controller

I'm quite new to ZF, and right now, i try to write a tiny app, based on ZF. It works more or less fine until now. I wanna access my db- data. For starters, i just want to use query-string, before I start messing araound with zend_db. So to keep a nice mvc-structure, I created application/models/IndexMapper.php
class Application_Models_IndexMapper{...}
it just contains one function by now to see if it works
public function test(){
return ('yay');
}
In my IndexController, which is working, i try to access my model by
$indexMapper = new Application_Models_IndexMapper();
$x = $indexMapper->test();
but the first line throws an
Fatal error: Class 'Application_Models_IndexMapper' not found in /path/to/application/controllers/IndexController.php on line 31
As I'm new, I don't understand the more complex tutorials and they don't help me fix my problem. What am I doing wrong? Do I have to include it somehow?
Thanks
edit: my application/bootstrap.php
<?php
defined('APPLICATION_PATH')
or define('APPLICATION_PATH' , dirname(__FILE__));
defined('APPLICATION_ENVIRONMENT')
or define('APPLICATION_ENVIRONMENT' , 'development');
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
$frontController->setParam('env', APPLICATION_ENVIRONMENT);
Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');
//Doctype
$view = Zend_Layout::getMvcInstance()->getView();
$view->doctype('HTML5');
$view->addHelperPath('App/View/Helper', 'App_View_Helper');
unset($frontController);
The structure for a model would be found under ./application/models/IndexMapper.php. In that file you would have the class As you named it and then the autoloading will work.
A great beginner tutorial would be found on www.akrabat.com
You have your class in the wrong place and have named it incorrectly.
Your class should be in application/models/Indexmapper.php and should look like this:-
class Application_Model_Indexmapper
{
public function test(){
return ('yay');
}
}
Then you call it thus:-
$indexMapper = new Application_Model_Indexmapper();
$x = $indexMapper->test();
Notice I dropped the 's' from the end of Models, it is not required and will cause an error as you found. Also the class is in the models folder, not modules. If you want to use modules then you need to read this and this from the manual.
Your bootstrap.php should look like this for a first, basic project:-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//Yes, it's empty!
}
Well, I guess my tutorial wasn't very helpful. I'll do as recommended, and start over from scratch. Thanks though

Resources