Page route CodeIgniter - php

I am using a reference book called PHP and MVC with CodeIgniter, and I’m following the examples of projects putting in my personal pc.It runs fine at my local PC and but gives some error at webserver.
Diagnosis: The problem in question is the routes of pages. in my personal pc works normally, but when I put it on the web server can not find the requested page.
http://orion.locadados.com.br/~wladi/Carrinho_Compras/
http://orion.locadados.com.br/~wladi/Carrinho_Compras/categoria/artigos-esportivos
The routes defined in my project are as default for any server, because they are in the project folder itself. Just do not put the line of code, it would not know which part of the code put online.
How can i fix this?
file .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
the file routes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
$route['default_controller'] = "home";
$route['categoria/(:any)'] = "home/categoria/$1";
$route['produto/(:any)'] = "produtos/detalhes_produto/$1";
$route['404_override'] = '';
/* End of file routes.php */
/* Location: ./application/config/routes.php */

Related

Run a Project on browser from IntelliJ IDE

I have opened a project that I downloaded in IntelliJIDEA IDE. The project structure is as follows. I am trying to run the userAuthenticationController.php controller's index() method to view the login page.
According to this project's config.php File, the base_url is provided as follows.
config.php
$config['base_url'] = 'http://localhost:9080/Internship-Management/Sourcecode/Codeigniter/';
I tried running this address in Chrome but am getting the following error.
userAuthenticationController.php
// Show login page
public function index() {
$this->load->view('login/loginView');
}
routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| https://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
| $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
| my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Do I need to change the path that I am using to load the controller in my browser?
Any suggestions in this regard will be highly appreciated.
Unfortunately, opening a project in IDEA/PhpStorm is not enough.
You still need to:
Install Apache (I think using XAMPP would be the best shot for you)
Configure it to run on port 9080 (according to the config file you posted)
Deploy the project to the Apache web root (manually, or by using a PhpStorm/IDEA deployment configuration)

Laravel controller action returns error 404

I'm getting a 404 error when trying to access a route linked to a controller action.
I have the route defined like this in my routes.php file.
Route::controller('error', 'ErrorsController');
The ErrorsController class looks as follows.
class ErrorsController extends BaseController {
public function __construct()
{
// vacio
}
public function getIndex()
{
return View::make('error.accessdenied');
}
public function getAccessDenied()
{
return View::make('error.accessdenied');
}
}
I have a view with a link to chek if it is working properly. The link is created as follows
{{ HTML::linkAction('ErrorsController#getAccessDenied', 'Error') }}
When I click on the link the page moves to the URL 'mytestdomain.com/error/access-denied' returning an 404 error, but when I access the URL 'mytestdomain.com/error' it works perfectly.
Any idea on what I'm doing wrong?
EDIT:
Running the command php artisan routes these are the routes pointing to ErrorsController:
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| | GET|HEAD error/index/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getIndex | | |
| | GET|HEAD error | | ErrorsController#getIndex | | |
| | GET|HEAD error/access-denied/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getAccessDenied | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE error/{_missing} | | ErrorsController#missingMethod | | |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
Only the sencond and the fourth ones are working.
It looks as though specifying the route in the way you have won't work. This type of routing only works for RESTful requests. See >http://laravel.com/docs/4.2/controllers#restful-resource-controllers>.
You might have to explicitly specify the route using Route::get/post.
Somehow I found the problem.
For some reason, my apache server doesn't rewrite mytestdomain.com/error/ * route. Probably is something related with the word error and the apache module mod_rewrite.
Anyway, defining the route as follows solves the problem.
Route::controller('fail', 'ErrorsController');

default routing in codeigniter

I'm exploring codeigniter. On app startup default controller is changed to load my controller.
Controller properly loads the view and that's fine, so I'm guessing routing works as expected, but when I use (manually type on address bar other method on same controller) same url pattern /controller/method I'm getting 404 error, either view exist.
Do have to change some default routing behavior or something else is problem?
Thanks
I dont know if you already removed index.php from your url pattern, assuming that's the case you should type inside browser address field index.php/controller/method. (if you manually type url as you describe)
If you on the other hand do not want to use index.php on every link you can consider to remove that, more info here.
Well this may be the because of index.php file as mentioned above or else if you would like get rid of index.php Kindly include .htaccess file in your application.
config/config.php - modifiy
$config['base_url'] = 'index.php'
$config['base_url'] = '' // set it to blank
For .htaccess file refer the below code
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
follow this
root_folder/.htaccess
to remove index.php in url
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
set base URL
root_folder/application/config/config.php
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'http://[::1]/my-project/';
removing index.php in url, even on request post in form
root_folder/application/config/config.php
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
set default controller, mine is 'home'
root_folder/application/config/routes.php
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
| my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'home';
after that, make sure that the all controller file name is capitalize.
also a class name.
this is also important mostly when you need to upload in a live
server.
root_folder/application/controllers/Home.php
<?php
/**
*
*
* #author Lloric Garcia <emorickfighter#gmail.com>
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends MY_Controller {
public function index() {
}
}
then this will be you url
http://[::1]/my-project/home
that is my set up even in live server
all of this came from
https://www.codeigniter.com/userguide3/index.html

Codeigniter one service with multiple access points, subdomains, htaccess

I am not exactly sure how to word this properly so I apologize in advance. I have a slightly unique setup, but at the same time not so unique. I want to have
api.domain.com
m.domain.com
domain.com
All working off the same codebase, but serving up different views, and working off different controller sets. However I do not want to duplicate my code base by making mirror copies of it in various directories specific to the sub domain itself. To me that is redundant, and the opposite of productive, since I would have to manage 3+ sets of models, libraries, and in some cases controllers. To maintain functionality across the various versions of the service.
Right now, what I have setup and working is through constant growth of the routes.php is a means of saying what controller is used when through a normal domain.
ie
domain.com
domain.com/m/
domains.com/api/
Which works for now, but I am trying to think of whats best for organization and future development of the service.
So in all my question is, how can I setup codeigniter to support this logic of using subdomains while keeping everything in one main code base. Is this plausible? If so how could it be achieved?
Ok, so after a comment made to my original post, pointing me to another post here on stack I came up with a nifty way of handling my issue. Its not exactly the answer found in the link more than a derivative there of based on the logic. As I have multiple sub domains I want to roll out each with its own set of functionality and needs, as well as controllers specific to its cause that should only be called from those subdomains.
That said my solution, for those who may stumble across it is, in the routes.php I ended up making a small function to get the HTTP_HOST split it up based on . and use it from there to my needs. My example is as follows.
Mind you I also replaced everything in the routes.php so its not just a straight line of $route['this/that'] = 'dir/controller';
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There area two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router what URI segments to use if those provided
| in the URL cannot be matched to a valid route.
|
*/
function whichSubRoute()
{
$subs = array(
"api"=>"api/",
"m"=>"m/"
);
$curr = $_SERVER['HTTP_HOST'];
$curr = explode('.', $curr);
if(array_key_exists($curr[0], $subs))
{
return array($curr[0], $subs[$curr[0]]);
}
return false;
}
//due to the the way this setup works, some controller references
//can be found multiple times (and in no particular order).
//also note due to this setup, each method has its own default and 404
$choiceRoute = whichSubRoute();
if($choiceRoute !== false)
{
if($choiceRoute[0]=="api")
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//start version 1 (mvp API)
$route['1.0/user/(:any)'] = $choiceRoute[1].'v1_userinfo/index/$1';
//controllers outside of "/api"
}
if($choiceRoute[0]=="m")
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
//start version 1 (mobile)
$route['welcome'] = $choiceRoute[1].'m_welcome';
$route['dashboard'] = $choiceRoute[1].'m_dashboard';
$route['user/(:any)'] = $choiceRoute[1].'m_userinfo/index/$1';
$route['reg'] =
//controllers outside of "/m"
$route['login/auth'] = 'login/auth';
$route['logout/mobile'] = 'logout/mobile';
//end version 1 (mobile)
}
}
else
{
$route['default_controller'] = "welcome";
$route['404_override'] = '';
}
/* End of file routes.php */
/* Location: ./application/config/routes.php */
Also keep in mind I do want default and 404 controllers for each subdomain
I suppose you can load different configs base on ENVIRONMENT constant.
http://ellislab.com/codeigniter/user-guide/libraries/config.html
You may load different configuration files depending on the current
environment. The ENVIRONMENT constant is defined in index.php, and is
described in detail in the Handling Environments section.
To create an environment-specific configuration file, create or copy a
configuration file in application/config/{ENVIRONMENT}/{FILENAME}.php
For example, to create a production-only config.php, you would:
Create the directory application/config/production/ Copy your existing
config.php into the above directory Edit
application/config/production/config.php so it contains your
production settings

CodeIgniter subfolders and URI routing

I’ve read the manual on URI routing and views and something is not clicking with me.
In my views folder, I have a subfolder called products. In there is a file called product_view. In my controller, I have:
function index() {
$data['title'] = 'Product Overview';
$data['main_content'] = 'products/product_view';
$this->load->view('templates/main.php', $data);
}
The template loads a header view, a footer view and a navigation view, plus the view as a main content variable.
In my URI routing, I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are two reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['scaffolding_trigger'] = 'scaffolding';
|
| This route lets you set a "secret" word that will trigger the
| scaffolding feature for added security. Note: Scaffolding must be
| enabled in the controller in which you intend to use it. The reserved
| routes must come before any wildcard or regular expression routes.
|
*/
$route['default_controller'] = "index_controller";
$route['laser-configurator'] = "configurator";
$route['news-and-events'] = "news";
$route['products/product-overview'] = "products/product_view";
$route['scaffolding_trigger'] = "";
/* End of file routes.php */
/* Location: ./system/application/config/routes.php */
This causes a 404 error when I try to go to domain.com/products/product-overview. Do I need to do something with my .htaccess? If so, what? Here is my .htaccess:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
I’d appreciate some specific help, as the documentation isn’t specific on how to address this. I’ve done a little searching in the forums, and didn’t see anything, but I’m posting this while I keep looking.
In CI URIs are routed by the name of your controller and the name of the method in the controller.
If the controller is named "products.php" the function for the corresponding view is called "index", the correct URI for that page is "/products".
So your code should be
$route['products/product-overview'] = 'products/index';
To use multi-level directory structure for your controllers use router extention talked about in this post, I have used it on occasion, and can vouch for it working.

Categories