codeigniter - Do I need a Controller for every URL? - codeigniter

I have a working project on Codeigniter 3. Now I have to build a FAQ page and I had this doubt: do I need a Controller for every URL?
It is, the FAQ page is a static page, but CodeIgniter generally routes URLs to Controllers, like domain/controller/method. But it seems a waste to build a Controller to only load the View.

No, it's not right way to make controller for every page. Just make one function which shows page by fetching data from database.
First of all make a table named pages in your database then save page_content, page_name, permalink for your different pages.
Now suppose your default controller is home, make a function in it with name page as below.
function pages( $permalink )
{
// get page data based on page_name passed in URL
$this->db->where( array( 'permalink' => $permalink ) );
$data['page'] = $this->db->get( 'pages' )->result();
// load view and pass page object to view
$this->load->view( 'view_file', $data );
}
Now same function will show different page content based on permalink passed in URL.
For example if URL is www.example.com/index.php/home/pages/faq then content of faq page will be shown.

Related

Laravel - Get HTML of current page or of a view according to a path

I have a view mypage.blade.php and a route.
The url is like : https://example.com/mypage/param1/param2. The route use param1 and param2 and generate the page.
Question 1
In that page, I try to get its HTML code. Is there a way to do it?. I tried render() but I don't get what I want.
Question 2
In the view, can I get the HTML code of an other view by specifying a path ?
You had the right idea. Not sure why it wouldn't work for you.
In the controller, set the view into a variable:
$view = view('myBaseView', compact('people', 'places', 'things'));
Now, if you dump the rendered view variable, you have the page's HTML:
dd($view->render());
To get the html of another view by specifying the path and using the internal controller, you would need to set up some kind of a wrapper or catch so that the view variable is not returned as a view, but rendered out to html as above. Your method would need to trap whatever the original controller was sending before it pushed out the view.
Of course, old school php can get the other page's rendered html too possibly if your server is set to allow this:
$html = file_get_contents('http://mypage.com/');
Something else you might find handy is the Laravel sections method. If you just want to render part of the page you can do so by calling whatever section you want from a partial view:
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
dd($sections['modalContent']); // this will only dump whats in the content section
I don't know what you want to do with this html, but if you wish to display it on a page, once you send it (you'd possibly want to return the view along with a compact of the variable $view... as a normal variable if so), remember to use this format:
{!! $view !!}
HTH

How to pass arguments directly to default controller in Codeigniter

I have one controller called "site" which is a default controller. I want to display all the post from my database. So whenever users go to my site, they can see all the posts. I have removed "index.php" and URL rewriting on through .htaccess.
There are two buttons called latest and popular and also some categories on that page. I just want whenever user click on latest or popular button or any category I want to pass the data directly to the default controller like this way-
http://example.com //Show all post from all categories
http://example.com/latest //Show the latest post from all categories
http://example.com/popular // Show the popular posts from all categories
http://example.com/health //Show all post from category health
http://example.com/health/latest //Show latest post from category health
http://example.com/health/popular //Show popular post from category health
Now my questing is how to write the routing part.
This is my default routing
$route['default_controller'] = 'site';
and here is my index method of site controller
public function index($cat=null, $type=null)
{
//The other logic here
}

LinkPager for ajax generated results

Is there a way to change the Url generated by the LinkPager in Yii2?
I have a page with results that are loaded with ajax when the search field is updated. But my pagination buttons links to a new page with the ajax-call url.
I looked in the files, and it seems like this function generates the url
public function createUrl($page, $pageSize = null, $absolute = false)
and that is not option to change. What is the way to solve this problem?
My results page is at the front page, but the pagination buttons links to ajax/results?page=2&page-size=10.
What can be more elegant then extending a class and replacing the renderPageButton function with your own :).
Another way, probably more the yii2 way :) if you take a look at LinkPager you will see that it is not responsible of creating the URLS. Pagination is https://github.com/yiisoft/yii2/blob/master/framework/data/Pagination.php#L257 The createUrl function in Pagination creates the URL and it takes into consideration the $route defined for the pagination object. If there is no $route defined then it takes the current controller's route. So by setting up $route for the pagination you can set the URL the buttons will go to.

Magento: How to use admin configforms in frontend?

I have an extension which should give the users (logged in as an Admin in the magento backend) the ability to change some configs in the frontend area. I want to have a link in the frontend which loads the config area via ajax and gives the user the possibility to edit&save this config in the loaded div. I want to use the magento backend forms for this so i don't have to code the forms myself.
My current approach has the link on the pages and loads via ajax the correct backend page (e.g. System > Configuration > Design). For this approach I created a Controller which extends the Mage_Adminhtml_Controller_Action. This Controller get the params from the ajax request and uses an action (like the editAction of the class Mage_Adminhtml_System_ConfigController) to get the right config page in the backend.
My Problems are:
- showing only the correct Area (I just want the user to edit only the section "themes" under System > Configuration > Design) everything else should be not available... so how to remove all the information around this config section?
The form needs the JS-variable Form_Key. How to get the current Form_Key (in the frontend)?
After the ajax has loaded the content the form doesnt get initialized correctly. So if I'm trying to submit the form my firebug says "JS-Error: configForm is not defined". How to solve this form initialising ? Any ideas?
I really hope anybody here can give me a hint how to solve this problems to get the backend config work in the frontend.
This is untested, but it should be enough to get you on the right track:
Output only a specific block
In the frontend most blocks are instantiated via layout XML. In the adminhtml area this is different, so you need to work with PHP instantiation much more.
In your AJAX action I assume you are currently calling loadLayout() and renderLayout().
To only output a specific section use this instead:
public function yourAjaxAction()
{
// assuming the required config section is set in the AJAX request
$sectionCode = $this->getRequest()->getParam('section');
$sections = Mage::getSingleton('adminhtml/config')->getSections();
$blockName = (string)$sections->frontend_model;
if (empty($blockName)) {
$blockName = Mage_Adminhtml_Block_System_Config_Edit::DEFAULT_SECTION_BLOCK;
}
$block = $this->getLayout()->createBlock($blockName)->initForm();
// Set the AJAX response content
$this->getResponse()->setBody($block->toHtml());
}
The form key
The form key can be fetched via
Mage::getSingleton('core/session')->getFormKey()
It must be present in the form posted back to the server. You can use the following code to create a HTML hidden field with the formkey:
// If loadLayout() was called:
$formkeyHtml = Mage::app()->getLayout()->getBlock('formkey')->toHtml();
// If working without layout XML:
$formkeyHtml = Mage::app()->getLayout()->createBlock('core/template', 'formkey')
->setTemplate('formkey.phtml') // adminhtml theme formkey
//->setTemplate('core/formkey.phtml') // frontend theme formkey
->toHtml();
Add configForm JavaScript
The configForm variable is an JS varienForm object of the DOM element containing the config fields.
It is instantiated using:
// config_edit_form is the CSS id
configForm = new varienForm('config_edit_form');
The varienForm declaration is in the file js/varien/form.js.
There also is some additional javascript used by the system configuration. Magento always adds in these blocks to set up the system config JS environment:
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/shipping/ups.phtml')
);
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/config/js.phtml')
);
Mage::app()->getLayout()->getBlock('js')->append(
$this->getLayout()->createBlock('adminhtml/template')
->setTemplate('system/config/applicable_country.phtml')
);
I hope that gets you started.

codeigniter:everything created through MVC pattern?

I'm studying codeigniter and I would realize a simple application. I'm asking if every page, even if doesn't not contain directly dynamic element must be create through MVC pattern? I explain myself: my home page will not contain anything of dinamic. only an header, menu and footer. it needs to create model,controller and view to handle this situation or I create simple the home page?
You always have to create a controller because that is what is called from the url.
As far as the view and model. You don't always have to create either.
I've got plenty of pages with static info so I don't need any model interaction at all.
Without a view you are kind of defeating the purpose of the MVC. It is possible for the controller to just echo all your html for the page but I wouldn't do it.
The way I do it is that I have a default view that contains the header and footer. A content view that all my content for the page goes into. I then pump my view for the page into the content view then that into the default view to create my page.
$arrData["vwsContent"] = $this->load->view("your view for the page", $arrData, TRUE);
$arrData["vwsPageContent"] = $this->load->view("content template view", $arrData, TRUE);
$this->load->view("default template view", $arrData, FALSE);
In this way I can have different content views but the same default view for all the pages. For instance my homepage looks different than my regular pages so I would have a HOME template to use instead of a CONTENT template.
You can define the home page function in any controller.
In routes.php the default controller and action can be defined
$route['default_controller'] = "welcome"; (welcome can be replaced by any your prefer controller) .
Create function with name index
function index(){
$this->load->view('index');
}
Then create the file index.php in "views" folder.
In index.php you can put all your HTML static content. You can use URL helper [ function base_url()] for images/css/js path.

Resources