URI routing in codeigniter - php

As the title suggests, my issue is with codeIgniter.
I have used a code in an .htaccess file to remove index.php which works all OK.
But I need to go further in changing the URI:
My main controller is page(), so when a user is in my homepage, the URL-bar shows:
www.example.com/page/
(because homepage is index page, it does not show the page name as usual the controller suffice),
but If I go to registration page, the URL-bar shows:
www.example.com/page/register
Up to here everything is OK, but I want the codeIgniter to show my domain without the page() when the user is in my homepage, I don't want foolish www.example.com/page/ to appear and I think when someone is visiting index page, the URL-bar better to be www.example.com

You can define a custom route in config/routes.php - for example:
$route['default_controller'] = 'page';
Then, http://example.com
goes to http://example.com/page
then if you did not specify any data , it will route to default controller.

for the link to register:
www.example.com/register
in config/routes.php
$route['register'] = 'page/register';

You have everything explained here in great detail.
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html there is an example here on the bottom how to do uri routing with routes.php file in codeigniter.

Related

How can I move the login behaviour from the /login URL to my home page in a Laravel application?

I am pretty new in PHP and moreover in Laravel framework (I came from Java) and I have the following problem related to Laravel security.
I have correctly configured my Laravel login system so if I access to the page:
http://localhost:8000/login
I obtain my login page from where I can access to the restriced area of my portal. This is the standard Laravel login page.
My doubt is: can I take this behavior into my home page? Because I have to put the login into a custom login from into my home page (the one that is automatically opened launching http://localhost:8000/).
How can I do this?
From your question i understand that:
You are adding the login form in the homepage and not trying to provide the link to the login page from homepage.
If this is not your question i could delete my answer.
So, if you are using blade you could easily import the chunks of codes from login page.
More on Sub-view Here:
https://laravel.com/docs/5.3/blade#including-sub-views
Example:
login.blade.php
<div class="form">
//your login form contents
</div>
homepage.blade.php
//your homepage codes
#include('login')
//rest of your homepage codes
Note:
The path to your subview is relative to resources/views/.
i.e. If you have this folder structure like this:
|-resources
|-views
|-homepage.blade.php
|-partials
|-login.blade.php
then you will need to use:
#include('partials.login')
in your homepage.blade.php to use that view.
Inside web.php add this route at the end of the routes
Route::get('/', 'Auth\AuthController#getLogin');
You can update routes file in laravel like below.
Basically your routes file is located in
YourProject/app/Http/routes.php
here you can copy the View(blade) / Controller of /login route to /. Like example shown below.
Route::get('/login',function() {
return view('login');
});
Replace the above one to
Route::get('/',function() {
return view('login');
});
example above is without controller.
Hope it works!!

How to call one controller(function) view with multiple url in codeigniter via routes.

I create a controller function home/index .
calling vai url it is showing me html design.
I want to call the view of home/index via custom urls.
If I type
www.example.com/home_new // it will open view of home/index
www.example.com/home_new1 // it will open view of home/index
Also i want to save the custum urls in database so that admin can change.
Please advise how to do this via routes or another method.
Add this code to inside route.php file:
$route['home_new'] = 'home/index';
$route['home_new1'] = 'home/index';
This will solve your problem. For database it will not help you.
You can take reference from this link.

how to load my default controller in codeigniter using bootstrap?

i have a view with bootstrap template which is embedded at my view/reportlist.php (localhost/Project/index.php). the problem is that every time i perform a CRUD, well codeigniter reroutes me to deffirent uri. example, when i edit a report from my list, i will be redirected to my view/reportlist.php (/Project/index.php/report/edit). then my bootstrap template is ruined. i need to go back to the /Project/index.php to load back everything up. please if you know, post some ideas, ive been like this for days now, i cant find a relevant answer.
there is no relation between codeigniter route and bootstrap. you can set you default controller in line 41 in: application > config > route.php file.
on ther other hand you can set you base_url in line 17 in: application > config > config.php by this:
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
I'm sure that you have call again the list view in you edit method in report controller :D .
if you want to avoid index.php from you url then you can modify code in your .htdocs files:
In your form action attribute specify a route: <?php echo base_url('/someRoute'); ?>, and in routes.php map it to some specific controller: $route['someRoute'] = 'home/someRoute';, from this controller load your desired view.

CodeIgniter - creating forms with .html link

I want to create a simple website with 5 pages which contains 2 simple contact us forms.
The links should be www.yoursite.com/contact.html and after submitting it should go to www.yoursite.com/success.html. Please help, I have searched a lot for it but couldn't find anything
[edit]
I have a different controllers for different forms but in the links i don't want the controller name but only the view name.html
If all you want to do is change the apperance of the URLs, then you could use CodeIgniter's routing. You'll most likely also want to remove index.php from the URL, more information is available in CodeIgniter's user guide.
For example, if you have a controller named Contact with an index() function that loads the view for the contact form, and another function success() that loads the success page view:
Then this route, in application/config/routes.php would map the URL, www.yoursite.com/contact.html to your Contact controller.
$route['contact.html'] = "contact";
This route would map www.yoursite.com/success.html to the success() function within the Contact controller.
$route['success.html'] = "contact/success";
If you want your Codeigniter URLs to have ".html" appended to them for some reason, you should modify your application\config\config.php file and change
$config['url_suffix'] = '';
to
$config['url_suffix'] = '.html';
You can see here for more info.

Having a tough time understanding Codeigniter URL

This is one of those things that just makes me feel like an idjut.
Ok - so I am just starting out with Codeigniter and I just am having a hell of a time getting my head around the URL system.
Here are some relevant config settings up front ( and I'm on MAMP/localhost at the moment )
$config['base_url'] = 'http://localhost:8888/MY_SITE/';
$config['index_page'] = 'index.php';
$route['default_controller'] = "home";
My .htaccess file is blank as of now.
So I have a "home" controller, which I hit fine, and it loads my "home_view". On it I have a registration link ...
<p>Not a member yet? Sign Up!</p>
This renders as the following HTML ...
Sign Up!
Everything fine so far. But when I click I get a "404 Page Not Found". I have a "registration.php" controller with an index method that loads a "registration_view.php".
This is what is showing in my address bar
http://localhost:8888/MY_SITE/index.php/registration
Why do I not hit it?
My logs show ..
ERROR - 2012-11-13 18:21:12 --> 404 Page Not Found --> registration/index
.. WHY DOES FATE HATE ME SO?
One of many possible and most close reason for 404 might be that there's no index() function in your controller. Can u post the code??
EDIT ( Since ans got downvote ) I have to give more explanations.
In this case possible reasons for 404 error can be -
Your default controller is set home and the url on which your are getting 404 is calling registration controller.
If there's no registration controller you get 404.
If there's no function named as registration present in your default home controller.
First arguement in URI (after index.php [if htaccess is not used to remove index.php] ) is for controller that will be called. And second argument after index.php is function that will be called from controller mentioned in first argument. In both cases, ie controller and function not present you get 404.
If registration is function and it's visibility is set to private then you get 404.
Just for note, to make URLs more readable you should include htaccess and add rules to remove index.php.

Categories