Hi i build a indexcontrollor in module brand like this
class Blank_Brand_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo 'Foo Index Action';
$this->addaction();
}
public function addAction()
{
echo 'Foo add Action';
$this->deleteAction();
}
}
When I put in the address: http://www.myshop.com/index.php/brand/, it echos Foo Index Action
With this URL, though, it does nothing: http://www.myshop.com/index.php/brand/add
What could be the problem here causing this? This could save a lot of problems for me which I have with URL rewriting in Magento!
It's a common oversight.
This url
http://www.myshop.com/index.php/brand/
is equivalent to this url
http://www.myshop.com/index.php/brand/index/index
The URI portion "brand" is your module. The first "index" URI portion is your controller, the second "index" URI portion is your action method.
Module: brand
Controller: index
Action: index
So, let's consider this URL
http://www.myshop.com/index.php/brand/add
This is equivalent to
http://www.myshop.com/index.php/brand/add/index
Which gives us
Module: brand
Controller: add
Action: index
The URL you're trying to call is looking for a controller named
class Blank_Brand_AddController ....
When it doesn't find one, it reports back 404.
If you wanted to call the addAction method on your index controller, you'd want the following URL
http://www.myshop.com/index.php/brand/index/add
I wrote an article on this - perhaps it would help: http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/
Related
What would be the best way to get uri segment via parameter in controller? i have used following code but not working
uri as
http://localhost/home/23
class Home extends CI_Controller
{
public function index($para){
echo $para;
}
}
Above uri results in 404 page not found error.How to solve such problem and get the id as parameter in index function.
You can write routing in your routes.php file.
File path : application/config/routes.php
$route['home/(:any)'] = 'home/index/$1';
OR
$route['home/index/(:any)'] = 'home/index/$1';
I hope it will help.
I have a PHP CodeIgniter Controller with name User and have a method that get details of user user_detail($username)
Now when i need to show user data for example for userName mike
I call this URL
http://www.example.com/user/user_detail/mike
My target
How to make user data accessible by next URLs
http://www.example.com/user/mike
or / and
http://www.example.com/mike
You have to read the this page from the official documentation of codeigniter. It covers all related things to Routing URLs easily. All routes must be configured via the file:
application/config/routes.php
It could be something like this :
$route['user/(:any)'] = "user/user_detail/$1";
This can be achieved by overriding CI_Controller class BUT dont change the original core files, like I said override the controller and put your logic in it.
Help: https://ellislab.com/codeigniter/user-guide/general/core_classes.html
how to create Codeigniter route that doesn't override the other controller routes?
Perhaps an easier solution would be to route it with the help of apache mod_rewrite in .htaccess
Here is an detailed explanation on how to achieve it: http://www.web-and-development.com/codeigniter-remove-index-php-minimize-url/
Hatem's answer (using the route config) is easier and cleaner, but pointing the usage of the _remap() function maybe helpful in some cases:
inside the CI_Controller, the _remap() function will be executed on each call to the controller to decide which method to use. and there you can check if the method exist, or use some defined method. in your case:
application/controllers/User.php
class User extends CI_Controller {
public function _remap($method, $params = array())
{
if (method_exists(__CLASS__, $method)) {
$this->$method($params);
} else {
array_unshift($params, $method);
$this->user_detail($params);
}
}
public function user_detail($params) {
$username = $params[0];
echo 'username: ' . $username;
}
public function another_func() {
echo "another function body!";
}
}
this will result:
http://www.example.com/user/user_detail/john => 'username: john'
http://www.example.com/user/mike ........... => 'username: mike'
http://www.example.com/user/another_func ... => 'another function body!'
but it's not going to work with: http://www.example.com/mike , since the controller -even if it's the default controller- is not called at all, in this case, CI default behaviour is to look for a controller called mike and if it's not found it will throws 404 error.
for more:
Codeigniter userguide 3: Controllers: Remapping Method Calls
Redirect to default method if CodeIgniter method doesn't exists.
I'm new to FuelPHP and I did a little coding with it! What I did was create a simple controller and created two methods. One for action_index() and the other is action_add().
the code is given below. Views are already in the app\views\ folder.
class Controller_Student extends Controller
{
public function action_index()
{
return Response::forge(View::forge('index'));
}
public function action_add()
{
return Response::forge(View::forge('select'));
}
}
I've set the root to this controller class. When I run the application the index works fine and loads the directed view. But when I give the following URL
http://localhost/project/public/add/
the method doesn't get called! A 404 error is give saying
You can see this page because the URL you are accessing cannot be found.
What Am I doing wrong here. I've gone through every documentation, tutorial I find but I shouldn't get this type of an error. Please help me.
Below is the routing file code :
return array(
'_root_' => 'student', // The default route
'_404_' => 'welcome/404', // The main 404 route
);
You've set the root to student controller, but that doesn't mean all traffic goes through that controller. Try visiting:
http://localhost/project/public/student/add/
This is my Zend Framework directory structure (only included what you'll need):
-Controllers
-HomeController.php
-IndexController.php
-Views
-scripts
-home
-index.phtml
-index
-index.phtml
In the IndexController I want to automatically redirect them to the home view.
How do I achieve this?
This is what I have tried so far:
header('/application/views/home');
I want to redirect to a different view and controller altogether. Not a different action within the same controller.
you have some options:
use the _redirect action utility method (very common)
public function indexAction() {
$this->_redirect('/application/views/home');
}
or use the _forward action utility method to just execute the requested action without actually redirecting
public function indexAction() {
$this->_forward('index', 'home');
}
you could use the action helper redirector, similar to _redirect but has many more options.
public function indexAction() {
$this->getHelper('Redirector')->gotoSimple('index','home');
}
This about covers the basics of redirecting inside of a controller, if you want to actually render home/index.phtml from /idex/index you could use the viewrenderer action helper.
I'll let you unlock those secrets.
If you want to render another action inside current action it is possible within your action the following way:
$this->render('actionName');
or, explicitly set the template to render
$this->renderScript('path/to/viewscript.phtml');
UPDATE: Reading again your question I suppose you want to use
_forward($action, $controller = null, $module = null, array $params = null): perform another action. If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed.
or
_redirect($url, array $options = array()): redirect to another location. This method takes a URL and an optional set of options. By default, it performs an HTTP 302 redirect.
see documentation on this question.
I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.