I would like to know if it's possible out of the box in CodeIgniter2 to create the urls without modifying routes.php for each controller's function like this:
backend.website.com/ecommerce/products/edit/$id
backend.website.com/ecommerce/currencies/form/$id
backend.website.com/ecommerce/customers/partners/etc/$arg1/$arg2
My controllers/ecommerce.php for function products() is something like this:
public function products($page = 0, $items = NULL, $subview = NULL)
{
if($subview != NULL){
// This function is localhost/ecommerce/products/form/$some_id
_remap($subview); // gives me an error
}else{
// Default view with pagination arguments $page and $items
// Page settings
$this->config->set_item('page_name', 'Products');
$this->config->set_item('page_subname', 'Product listing table');
// Request items
$this->data['items'] = $this->ecommerce_model->get_products(array('page' => $page, 'items' => $items));
// Creating a view
$this->data['view'] = $this->load->view('/ecommerce/products/index', $this->data, TRUE);
$this->load->view('/_templates/default/index', $this->data);
}
}
public function _remap($method)
{
if ($method == 'form')
{
$this->$method();
}
else
{
// SKIP
}
}
I found that default _remap() function could be useful, but I do not understand how to use it with my function.
Does anyone have any experience with that and could provide some little sample?
==== UPDATED ====
Is it even possible for _remap() to work with another functions such as orders(), customers(), currencies(), etc... at the same time?
You don't need to complicate things that much for something like this.
Just add another parameter to each of your methods, for example $action:
public function products($action = false, $page = 0, $items = NULL, $subview = NULL) {
switch($action) {
case 'edit':
// edit stuff here
break;
case 'something else':
// other stuff
break;
}
// etc...
}
Related
I'm trying to follow the instructions on this docs page, but I seem to be missing something:
https://docs.joomla.org/Supporting_SEF_URLs_in_your_component
The URI that needs to be corrected is: index.php?com_component&view=legal&page=customermasteragreement
It seems like the routing function should be simple, but the page is just displaying default instead of the sub-view.
Here's my current code:
function ComponentBuildRoute(&$query)
{
$segments = array();
if (isset($query['view'])) {
$segments[] = $query['view'];
unset($query['view']);
}
if (isset($query['page'])) {
$segments[] = $query['page'];
unset($query['page']);
}
return $segments;
}
function ComponentParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'legal':
$vars['view'] = 'legal';
break;
case 'customermasteragreement':
$vars['page'] = 'customermasteragreement';
break;
}
return $vars;
}
Update
This code works to display the subpage, but it gives me a URI like: legal-agreements/legal?page=customermasteragreement
class ComponentRouter extends JComponentRouterBase {
public function build(&$query) {
$segments = array();
$view = null;
if (isset($query['view'])) {
$segments[] = $query['view'];
$view = $query['view'];
unset($query['view']);
}
if (isset($query['id'])) {
if ($view !== null) {
$segments[] = $query['id'];
} else {
$segments[] = $query['id'];
}
unset($query['id']);
}
return $segments;
}
public function parse(&$segments) {
$vars = array();
// View is always the first element of the array
$vars['view'] = array_shift($segments);
return $vars;
}
}
EDIT 2
If it helps, here's my model and views
models/legal.php
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
class ComponentModelLegal extends JModelItem {
public function __construct($config = array())
{
JLoader::register('ComponentHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/component.php');
parent::__construct($config);
}
/**
*
* #return string
*/
public function getLegal() {
$app = JFactory::getApplication();
$page = $app->input->get('page', '', 'STRING');
if ($page) {
ComponentHelper::add('type', $page); //This is an API request to an external service, returning JSON formatted data
$legal = ComponentHelper::getData('commons/legal-agreements.json', TRUE);
if (isset($legal[0]['status'])) {
JError::raiseError(400, $legal[0]['ERROR']);
return false;
} else {
if (!isset($this->legal)) {
$this->legal = $legal;
}
return $this->legal;
}
}
}
}
views/legal/view.html.php
class ComponentViewLegal extends JViewLegacy {
function display($tpl = null) {
// Assign data to the view
$this->legal = $this->get('legal');
// Check for errors.
if (count($errors = $this->get('Errors'))) {
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
return false;
}
// Display the view
parent::display($tpl);
}
}
views/legal/tmpl/default.php
$page = JRequest::getVar('page');
$pages = array(
'resellermasteragreement',
'customermasteragreement',
'resellerdomainagreement',
'customerdomainagreement',
'resellerwebserviceagreement',
'customerwebserviceagreement',
'resellerdigicertagreement',
'customerdigicertagreement',
'registraragreement',
'customerhostingproductagreement',
'resellerhostingproductagreement'
);
?>
<div class="col-lg-12">
<?php
echo in_array($page, $pages) ? $this->loadTemplate('legal') : $this->loadTemplate('home');
?>
</div>
views/legal/tmpl/default_legal.php
$page = JRequest::getVar('page');
echo nl2br(htmlspecialchars($this->legal[$page]['defaultagreement'], ENT_NOQUOTES, "UTF-8"));
?>
<a type="button" class="btn btn-primary" href="<?php echo JROUTE::_("index.php?option=com_component&view=legal"); ?>">Back</a>
EDIT 3
This works because I wasn't navigating directly to the page from a menu item. There's a top level menu, then a page a links to the agreements. I have a hidden menu to each item, but that wasn't the link I followed.
$app = JFactory::getApplication()->getMenu();
$customermaster = $app->getItems( 'link', 'index.php?option=com_component&view=legal&page=customermasteragreement', true );
JRoute::_('index.php?Itemid='.$customermaster->id);
You have one view but you're not actually set the page argument correctly when you catch a view argument, but you treat page as some king of another view. Can you please try this and check if it works:
function [componentname]ParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'legal':
$vars['view'] = 'legal';
$vars['page'] = $segments[1];
break;
}
return $vars;
}
Also the functions ComponentBuildRoute, ComponentParseRoute must have your components name instead of Component at the beginning.
EDIT
[componentname]BuildRoute and [componentname]ParseRoute are deprecated, you should stick the the class that extends JComponentRouterBase as you have it in your updated second example. Try this one here and see if it works:
class ComponentRouter extends JComponentRouterBase {
public function build(&$query) {
$segments = array();
$view = null;
if (isset($query['view'])) {
$segments[] = $query['view'];
$view = $query['view'];
unset($query['view']);
}
if (isset($query['page'])) {
$segments[] = $query['page'];
unset($query['page']);
}
return $segments;
}
public function parse(&$segments) {
$vars = array();
// View is always the first element of the array
$vars['view'] = array_shift($segments);
if(count($segments) > 0)
$vars['page'] = array_shift($segments);
return $vars;
}
}
EDIT 2
I have a test Joomla 3 site with a simple component named Ola.
Non SEO component URL: http://j3.dev.lytrax.net/index.php?option=com_ola&page=test_page&view=ola
URL generated by JRoute before Router class added to router.php: http://j3.dev.lytrax.net/index.php/component/ola/?page=test_page&view=ola
SEO URL returned by JRoute::_('index.php?option=com_ola&page=test_page&view=ola'); after creating router.php and used the Router class above: http://j3.dev.lytrax.net/index.php/component/ola/ola/test_page
If you visit the links, you'll see that all are working and you can even see the page, view and JRoute results of the calling component Ola.
Unless I've completely misunderstood Joomla (I've been developing with it for four five years now), there is no "sub-view" concept implemented. Trying to use the idea is what's getting you into trouble I think.
I'm astounded that the idea of using a visual debugger is so unpopular.
Get yourself something like Netbeans (there are others too), set up a local web and database server, and watch the code run. Very (well, relatively very) quickly you'll start to understand how the structure hangs together.
You may put code in your view that picks up the extra params you've set and displays or hides things accordingly, but there is no "sub-view".
In .NET using AttributeRouting we can add the Route for each Action Method. Something like below
[HttpGet, Route("Create-Project")]
public ActionResult CreateProject()
{
return View();
}
So, in the above code...line 1 indicates that we can mention the route for each Action Method. So url will become something like below..
http://domainname/Create-Project
Question
Is it feasible in PHP MVC CI ? and right now I will have to write the code in route.php in config folder.
In routes.php you can define it
Syntax
$route['short_url_name'] = 'complete/path/for/url';
Example
$route['Contact-Us'] = 'home/contact';
$route['Blog'] = 'home/blog';
$route['Our-Work'] = 'home/work';
so URL look like
http://domain.com/Contact-Us
http://domain.com/Blog
http://domain.com/Our-Work
Codeigniter.com examples
In Application/config/router.php you can add some config like that:
$route['Create-Project'] = 'home/welcome';
So, each time you visit http://domain.com/Create-Project, it will redirect you to http://domain.com/home/welcome
If you are looking for database driven dynamic routes you can rty this, else you can follow what #abdulla has suggested.
Table structure for Dynamic routes(Table name example: routes).
id | slug |route
---------------------------------------------
1 |Pankaj |controller/method/id of user
2 |Abdulla |controller/method/id of user
3 |Niranjan |controller/method/id of user
In controller
$this->load->helper('text');
$slug = $this->input->post("username");// example if you want the username in url as route.
$slug = url_title(convert_accented_characters($slug), 'dash', TRUE);
$slug = $this->Routes_model->validate_slug($slug);
$route['slug'] = $slug;
$route_id = $this->Routes_model->save($route);
$route_id will have the id of route in routes table, insert the route id and slug in users table(For which table you want dynamic routes)
This is the code for route model
<?php
class Routes_Model extends CI_Model {
var $file_name;
function __construct()
{
parent::__construct();
$this->file_name = APPPATH.'config/routes'.EXT;
}
function check_slug($slug, $id=false)
{
if($id)
{
$this->db->where('id !=', $id);
}
$this->db->where('slug', $slug);
return (bool) $this->db->count_all_results('routes');
}
function validate_slug($slug, $id=false, $count=false)
{
if(is_numeric($slug))
{
$slug = '';
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i < 3; $i++)
{
$slug .= $characters[rand(0, strlen($characters) - 1)];
}
}
if($this->check_slug($slug.$count, $id))
{
if(!$count)
{
$count = 1;
}
else
{
$count++;
}
return $this->validate_slug($slug, $id, $count);
}
else
{
return $slug.$count;
}
}
function save($route)
{
// print_r($route);exit;
if(!empty($route['id']))
{
$this->db->where('id', $route['id']);
$this->db->update('routes', $route);
return $route['id'];
}
else
{
$this->db->insert('routes', $route);
return $this->db->insert_id();
}
}
}
?>
One condition i have checked is if the username is a number, i ll generate a length of 3 characters in validate_slug().
Now add the following code in core folder by file name MY_Router.php
<?php
class My_Router extends CI_Router
{
function __construct()
{
parent::__construct();
}
// this is here to add an additional layer to the routing system.
//If a route isn't found in the routes config file. then it will scan the database for a matching route.
function _parse_routes()
{
$segments = $this->uri->segments;
$segments = array_splice($segments, -2, 2);
// Turn the segment array into a URI string
$uri = implode('/', $segments);
// Is there a literal match? If so we're done
if (isset($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}
// Loop through the route array looking for wild-cards
foreach ($this->routes as $key => $val)
{
// Convert wild-cards to RegEx
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// Do we have a back-reference?
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
return $this->_set_request(explode('/', $val));
}
}
//look through the database for a route that matches and apply the same logic as above :-)
//load the database connection information
require_once BASEPATH.'database/DB'.EXT;
if(count($segments) == 1)
{
$row = $this->_get_db_route($segments[0]);
if(!empty($row))
{
return $this->_set_request(explode('/', $row['route']));
}
}
else
{
$segments = array_reverse($segments);
//start with the end just to make sure we're not a multi-tiered category or category/product combo before moving to the second segment
//we could stop people from naming products or categories after numbers, but that would be limiting their use.
$row = $this->_get_db_route($segments[0]);
//set a pagination flag. If this is set true in the next if statement we'll know that the first row is segment is possibly a page number
$page_flag = false;
if($row)
{
return $this->_set_request(explode('/', $row['route']));
}
else
{
//this is the second go
$row = $this->_get_db_route($segments[1]);
$page_flag = true;
}
//we have a hit, continue down the path!
if($row)
{
if(!$page_flag)
{
return $this->_set_request(explode('/', $row['route']));
}
else
{
$key = $row['slug'].'/([0-9]+)';
//pages can only be numerical. This could end in a mighty big error!!!!
if (preg_match('#^'.$key.'$#', $uri))
{
$row['route'] = preg_replace('#^'.$key.'$#', $row['route'],$uri);
return $this->_set_request(explode('/', $row['route']));
}
}
}
}
// If we got this far it means we didn't encounter a
// matching route so we'll set the site default route
$this->_set_request($this->uri->segments);
}
function _get_db_route($slug)
{
return DB()->where('slug',$slug)->get('routes')->row_array();
}
}
The above file is very important. As codeigniter first runs files inside core folder, this will create the slug you want. No need to add any routes in routes.php file.
Now while displaying users, you shud echo the slug in your users table.
Lets say controller name is users, method name is edit_user.
your controller code will look like this,
function edit_user($id){
// fetch the data related to $id, and load the view here
}
In url if it sees Niranjan, it will automatically take the route from routes table and will enter your function, edit_user().
Yes Method Looks Lengthy, but works perfect, i use this in all my ecommerce sites.
I do not know how to set a callback function for the view record page in codeigniter.
I use the callback_column function and it does what I need in the grid view, but on the view record page it does not work.
I searched their site and forum and did not found anything that could help me.
My code looks like:
$zeus = new grocery_CRUD();
$zeus->set_theme('bootstrap');
// $zeus->set_language('romanian');
$zeus->set_table('programari');
$zeus->columns(array('id_client', 'id_sala', 'denumire', 'numar_persoane', 'observatii'));
$zeus->callback_column('id_sala',array($this,'_test_function'));
$cod = $zeus->render();
$this->_afiseaza_panou($cod);
public function _test_function($row, $value)
{
return '0';
}
write this lines in \libraries\Grocery_CRUD.php
at line number 3530
protected $callback_read_field = array();
than put this function after constructor call
public function callback_read_field($field, $callback = null)
{
$this->callback_read_field[$field] = $callback;
return $this;
}
//Now update this function to manage the field outputs using callbacks if they are defined for the same
protected function get_read_input_fields($field_values = null)
{
$read_fields = $this->get_read_fields();
$this->field_types = null;
$this->required_fields = null;
$read_inputs = array();
foreach ($read_fields as $field) {
if (!empty($this->change_field_type)
&& isset($this->change_field_type[$field->field_name])
&& $this->change_field_type[$field->field_name]->type == 'hidden') {
continue;
}
$this->field_type($field->field_name, 'readonly');
}
$fields = $this->get_read_fields();
$types = $this->get_field_types();
$input_fields = array();
foreach($fields as $field_num => $field)
{
$field_info = $types[$field->field_name];
if(isset($field_info->db_type) && ($field_info->db_type == 'tinyint' || ($field_info->db_type == 'int' && $field_info->db_max_length == 1))) {
$field_value = $this->get_true_false_readonly_input($field_info, $field_values->{$field->field_name});
} else {
$field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
}
if(!isset($this->callback_read_field[$field->field_name]))
{
$field_input = $this->get_field_input($field_info, $field_value);
}
else
{
$primary_key = $this->getStateInfo()->primary_key;
$field_input = $field_info;
$field_input->input = call_user_func($this->callback_read_field[$field->field_name], $field_value, $primary_key, $field_info, $field_values);
}
switch ($field_info->crud_type) {
case 'invisible':
unset($this->read_fields[$field_num]);
unset($fields[$field_num]);
continue;
break;
case 'hidden':
$this->read_hidden_fields[] = $field_input;
unset($this->read_fields[$field_num]);
unset($fields[$field_num]);
continue;
break;
}
$input_fields[$field->field_name] = $field_input;
}
return $input_fields;
}
than call same as other callback functions
As far as I'm aware GroceryCRUD doesn't provide callbacks or another means of overriding the default output in the view state.
The solution to customising this would be to create a custom view to which you will insert the data from your record. This way you can customise the layout and other presentation.
What you would then do is unset the default read view with:
$crud->unset_read();
And add a new action where there are details on how to do this here.
What to do with the new action is point it to a URL that you map in routes.php if necessary and handle it with a new function in your controller. You'll either have to write a model function to retrieve the data since this isn't passed from GC or you can use the action to target a callback and feed $row to it via POST or something so that the data for the record is accessible in the view. (Look at the example in the link above).
What is a simple way in CodeIgniter that I can return a specific content type for request URL extension? For example I want to return json if the url is http://example.com/phone/digits/1.json, html if the URL ends in /1 or /1.html, and XML if the URL ends in /1.xml. This will load a view in the format specified. So in the above example (phone/digits/1.json) would return the json version of the digits method. Here is what I've got so far that is NOT correctly working but gives an idea of what I'm going for. It's currently generating a 404 if no arguments are passed (/phone/digits.json)... Any suggestions would be appreciated.
class Phone extends CI_Controller {
public $layout = FALSE;
public function __construct()
{
if (preg_match('/\.(html|json)$/', $ci->uri->uri_string(), $matches))
{
$this->format = ('html' == $matches[1] || !isset($matches[1])) ? '' : '.json.php';
}
}
public function digits()
{
$this->load->view('phone/digits' . $this->format);
}
Updated for clarity,
I didint understand your question well, asuming you want to simplify your url
its solution for this url : http://domain.com/phone/digits.json (xml or html also)
But with few modification , it can be useful also with http://domain.com/phone/digits/n.json (n - id number)
in config/routes.php
$route['phone/digits.(json|html|xml|php)'] = 'Phone/digits/$1';
$route['phone/digits'] = 'Phone/digits';
Controller
class Phone extends CI_Controller {
public $layout = FALSE;
public function __construct()
{
//hmm
}
public function digits($format = '')
{
if($format == '') {
//default view or something else
}
else {
$this->load->view('phone/digits' . $format);
}
}
Parse the url
grab the extension
use a switch condtional and set relevant output
-
switch( $extension ){
case 'json':
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
break;
case 'xml':
$this->output
->set_content_type('application/xml')
->set_output(file_get_contents(some_xml_file.xml));
break;
// etc etc
}
I am trying to take advantage of the Custom URL Rule Classes in Yii r1.8
I am trying to take something that looks like orgs/view/id/24 and instead display the name of the org as identified by Name in the db (i.e. changing www.mysite.com/orgs/view/id/24 to www.mysite.com/jaysshop dynamically ). Unfortunately I am not getting it to work.
Here is my code:
class OrgsUrlRule extends CBaseUrlRule
{
public $connectionID = 'db';
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route==='orgs/view/id') //even tried 'orgs/view' or 'orgs/index'
{
if (isset($params['Name']))
return $params['Name'];
else if (isset($params['Name']))
return $params['Name'];
}
return false;
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
{
}
return false;
}
}
urlManager:
array(
'class' => 'application.components.OrgsUrlRule',
'connectionID' => 'db',
),
I'm giving you this example in the assumption that you want to take the name of a shop from the URL trigger the your controller/action on it
For example:
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
$aParts = explode('/', $pathInfo);
if (count($aParts) == 1) // It's only 1 piece, so a possible "shop name"
{
if (isAValidNameForAShop($aParts[0]))
{
$_REQUEST['id'] = $aParts[0]; // Store it to retrieve it in the controller
return 'orgs/view';
}
}
return FALSE; // Seems like something else, we don't apply
}
Hope that helps you along a bit.