CodeIgniter: show a view by typing its url - php

A newbie here: sorry if the question is too simple, been looking in tutorials but I don't seem to look properly.
I have the
$route['default_controller'] = "mainpage";
in routes.php working properly, and now I just want to view one of the php pages in views folder by typing its url:
http://myserver/folder/thepageIwantToSee
I get a 404 Not Found error. Do I have to use a controller? I just want to see it first before any linking, is there a way to do it?
Thanks a lot!

class yourcontroller extends CI_Controller {
function pageiwanttosee()
{
code
code
$this->load->view('the_view_page', $data);
}
}

Yes, you can write a controller that displays the view specified.
http://myserver/showfile/folder/thepageIwantToSee , where showfile is your controller.
Don't forget to set some security check or disable this on production site.

Related

Routing with codeigniter with customized URL

I have a controller named home, which is my default controller too.
This is my path
http://192.168.1.100/FMP/mobile/home/index/suzuki-violin-school--6/102271
Now i am trying to rewrite the url as
http://192.168.1.100/FMP/mobile/suzuki-violin-school--6/102271
Where i need to remove both controller name and function name.
So far i tried putting this in route.php
$route['(?!user|product).*'] = "mobile/home/index/";
But it takes all other methods in home controller and other controllers to the index function of home controller.
Any thoughts on how can i achieve this?
Try this below code in end of your routes.php file
$route['(:any)/(:num)'] = 'home/index/$1/$2';
above route get two parameter and pass it into home page index function and your index function will be ass follow
public function index($parms1,$parms2)
Hope it will solve your problem. If not let me know. I'll happy to guide you through

404 href error PHP script

Im new to laravel, but im trying to integrate an existing php script that I have into the laravel app. I understand everything happens in a MVC based architecture. However,im trying to link this page into the header page whose path is application/view/templates/header.php.
For example, there is a controller present in application/controller/LoginController.php and that has a function called public function index() and the way which it is called from headertemplate is:
<li <?php if (View::checkForActiveControllerAndAction($filename, "login/register")) { echo ' class="active" '; } ?> >
Register
</li>
<li>
Administrator Login
</li>
As you can see when i try to call a script which is in the root directory, called admin.php it gives me a 404 - Page not found.
Im really struggling I hope someone can help me figure out the problem. A 404 error is never fun as it is only a tiny error.
If you really want to run a basic PHP script then you can always chuck it in the public directory or try routing it from somewhere else. You're using it as a login script for admins so I would highly suggest just reprogramming it to be a part of your laravel site. Totally up to you though, this may not be the best answer but it will work.
I would suggest do it step by step
Route
In your /app/Http/routes.php
Route::get('/login/register', 'LoginController#index');
Controller
Let make sure you create a LoginController.php in /app/Http/Controllers/
Function
In your /app/Http/Controllers/LoginController.php , create a function call index to return a view.
public function index(){
return view('templates.header); // <-------- /templates/header.blade.php
}
View
inside your /resources/views/templates create header.blade.php
Place all the HTML code needed in it
Test
go to http://localhost/login/register
should load what you place in side your header.blade.php
You shouldn't get 404 anymore.

codeigniter issue with loading view from controller

I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";

Why redirect function in CodeIgniter not working?

I have a really weird problem. I have been using CI for more than 2 years and it's the first time I found this kind of problem. I found that the redirect function is not working properly. I already load the url helper in autoload.php.
$autoload['helper'] = array('url','html','form','file');
What I want to do is to redirect the users to a controller if they call admin controller.
class Admin extends CI_Controller {
function index(){
redirect('secure');
}
}
But what happen is the page redirected to the base_url. I've tried adding the second parameter of redirect with refresh or location but the result is just the same. It also happens to all redirect function in other controller. Can anyone tell me what the cause of this problem to happen?
EDIT:
For additional information that might be helpful for finding the problem, here I put the code of the routes.php.
$route['default_controller'] = "frontpage";
$route['404_override'] = 'errors/page_missing';
$route['admin'] = 'secure';
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
function index()
{
redirect('secure', 'refresh');
}
}
I just figured out that the problem is caused by a function in my helper. By coincident, I use site_url() function in the helper, which is already used by the CI. I replace that function with another name and now the redirection is working perfectly. I don't know how it can affects the redirect function, though. Thanks for anybody tried to help me.
I also got this issue in my project and i found some white space at the bottom of my page after closing php tag.
There must be some white space or some kind of echo before your redirect() method or at the bottom of the page.
If this is not the issue then set log threshold to 4 in 'config/config.php' and check 'application/logs' folder.
I had the same problem of redirect function's.
I found the solution by checking it's log file. Which is in \application\logs\
Then find the file which is addressed in log file and check if there is any space or any unused blank lines 'before and after the php tag "" '
If you find any space then remove it. And your redirect function would be work.
Thanks.
it may be the absence of index.php on URL
that is....,
http://localhost/codigniter/index.php/controll_page/function_name
so.,
redirect will be.....!!
redirect(base_url()."index.php/controll_page/function_name");

Change Url Path from /auth/ CodeIgniter and Ionauth

This is probably a really simple question but I am entirely new to codeigniter. I am using ionauth for a secure account system but I would like to change the url path, simply changing the class name is not working, it simply gives me a 404 whenever I try access it. I am trying to change the /auth/ location to /account/ but when I try it by just renaming class Auth extends CI_Controller { to class Account extends CI_Controller { it does not work as expected.
Any suggestions are greatly appreciated,
Again I am new to this so please go easy it's probably a simple fix and me not know code igniter very well.
Thanks,
Simon
From application/config/config.php file ,you can set your base URL.Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url() will output the above string.
For url routing you can use like this $route['account/login'] = "auth/login"

Categories