Answer for first Question
CodeIgnitor is oop concept framework, which use Model View Controller.(MVC architecture).
So each and every click on site it will goes to Controller. Then only controller will decide what to show or what to do next.
If there are 3 pages (ex: home, product, contact us) then use 3 controllers(home=use default controller, product= use product
, contact us = use contact
) to it.
So then your URL will be (if you click product) it shows www.test.com/product
, if you click Contact it shows www.test.com/contact
.
Answer for second Question
IN your table always maintain auto-increment id always(MUST). Then you can pass id to controller and can get data you want.
for ex. i load some content to page.(content on product).
your data showing page(view)
<?php
foreach ($product as $variable) // $product is data array which you send data
{
?>
<div class="content">
<a href="[call your controller here, in my `product`]/[*call sub function*, i use `show`]/ [*then pass here to product id*]<?php echo $variable['id']"></a>
<div>
<?php
}
?>
So your URL look like(after click on product, Assume id of that product is 25) www.test.com/product/show/25
then in product controller create
public function show($id)//$id variable to assign value which coming through URL
{
1. your code
2. then load view
}
TRIED AND TESTED
screenshot here
you can display your helloworld template in your custom module, done some minor modifications to your code.
module declaration:
<?xml version="1.0"?>
<config>
<modules>
<M4U_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</M4U_HelloWorld>
</modules>
</config>
creating the folder structure and add the files
A. app/code/local/M4U/HelloWorld/etc/config.xml
<config>
<modules>
<m4u_helloworld>
<version>0.1.0</version>
</m4u_helloworld>
</modules>
<frontend>
<routers>
<helloworld>
<use>standard</use>
<args>
<module>M4U_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
<layout>
<updates>
<helloworld module="M4U_HelloWorld">
<file>M4U_HelloWorld.xml</file>
</helloworld>
</updates>
</layout>
</frontend>
<global>
<blocks>
<helloworld>
<class>M4U_HelloWorld_Block</class>
</helloworld>
</blocks>
</global>
</config>
B. app/code/local/M4U/HelloWorld/block/HelloWorld.php
class M4U_HelloWorld_Block_HelloWorld extends Mage_Core_Block_Template {
}
C. app/code/local/M4U/HelloWorld/controllers/IndexController.php
class M4U_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
echo 'hello world';
$this->loadLayout(); //This function read all layout files and loads them in memory
$this->renderLayout();
}
}
D. app/design/frontend/ * theme base* / * mytheme * /layout/M4U_HelloWorld.xml
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<helloworld_index_index>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
<reference name="content">
<block type="helloworld/helloworld" name="hello" template="helloworld/helloworld.phtml"/>
</reference>
</helloworld_index_index>
</layout>
E. app/design/frontend/ * theme base* / * mytheme * /template/helloworld/helloworld.php
echo 'im a template block';
Best Answer
First and foremost, I highly recommend you buy the PDF/E-Book from PHP Architect. It's US$20, but is the only straightforward "Here's how Magento works" resource I've been able to find. I've also started writing Magento tutorials at my own website.
Second, if you have a choice, and aren't an experienced programmer or don't have access to an experienced programmer (ideally in PHP and Java), pick another cart. Magento is well engineered, but it was engineered to be a shopping cart solution that other programmers can build modules on top of. It was not engineered to be easily understood by people who are smart, but aren't programmers.
Third, Magento MVC is very different from the Ruby on Rails, Django, CodeIgniter, CakePHP, etc. MVC model that's popular with PHP developers these days. I think it's based on the Zend model, and the whole thing is very Java OOP-like. There's two controllers you need to be concerned about. The module/frontName controller, and then the MVC controller.
Fourth, the Magento application itself is built using the same module system you'll be using, so poking around the core code is a useful learning tactic. Also, a lot of what you'll be doing with Magento is overriding existing classes. What I'm covering here is creating new functionality, not overriding. Keep this in mind when you're looking at the code samples out there.
I'm going to start with your first question, showing you how to setup a controller/router to respond to a specific URL. This will be a small novel. I might have time later for the model/template related topics, but for now, I don't. I will, however, briefly speak to your SQL question.
Magento uses an EAV database architecture. Whenever possible, try to use the model objects the system provides to get the information you need. I know it's all there in the SQL tables, but it's best not to think of grabbing data using raw SQL queries, or you'll go mad.
Final disclaimer. I've been using Magento for about two or three weeks, so caveat emptor. This is an exercise to get this straight in my head as much as it is to help Stack Overflow.
Create a module
All additions and customizations to Magento are done through modules. So, the first thing you'll need to do is create a new module. Create an XML file in
app/modules
named as followsMyCompanyName is a unique namespace for your modifications, it doesn't have to be your company's name, but that the recommended convention my magento.
HelloWorld
is the name of your module.Clear the application cache
Now that the module file is in place, we'll need to let Magento know about it (and check our work). In the admin application
Now, we make sure that Magento knows about the module
If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is more frustrating then forgetting the clear out the cache and wondering why your changes aren't showing up.
Setup the directory structure
Next, we'll need to setup a directory structure for the module. You won't need all these directories, but there's no harm in setting them all up now.
And add a configuration file
and inside the configuration file, add the following, which is essentially a "blank" configuration.
Oversimplifying things, this configuration file will let you tell Magento what code you want to run.
Setting up the router
Next, we need to setup the module's routers. This will let the system know that we're handling any URLs in the form of
So, in your configuration file, add the following section.
What you're saying here is "any URL with the frontName of helloworld ...
should use the frontName controller MyCompanyName_HelloWorld".
So, with the above configuration in place, when you load the helloworld page above, you'll get a 404 page. That's because we haven't created a file for our controller. Let's do that now.
Now try loading the page. Progress! Instead of a 404, you'll get a PHP/Magento exception
So, open the file we just created, and paste in the following code. The name of the class needs to be based on the name you provided in your router.
What we've just setup is the module/frontName controller. This is the default controller and the default action of the module. If you want to add controllers or actions, you have to remember that the tree first part of a Magento URL are immutable they will always go this way
http://example.com/magento/index.php/frontName/controllerName/actionName
So if you want to match this url
You will have to have a FooController, which you can do this way :
Please note that the default controller IndexController and the default action indexAction can by implicit but have to be explicit if something come after it. So
http://example.com/magento/index.php/helloworld/foo
will match the controller FooController and the action indexAction and NOT the action fooAction of the IndexController. If you want to have a fooAction, in the controller IndexController you then have to call this controller explicitly like this way :http://example.com/magento/index.php/helloworld/index/foo
because the second part of the url is and will always be the controllerName. This behaviour is an inheritance of the Zend Framework bundled in Magento.You should now be able to hit the following URLs and see the results of your echo statements
So, that should give you a basic idea on how Magento dispatches to a controller. From here I'd recommended poking at the existing Magento controller classes to see how models and the template/layout system should be used.