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
Related
I'm trying to navigate to a page I created for the logged in profile of a doctor. I have not put in any authentication as I want to take care of the front end first then move to that part of the project. So basically I wanted to navigate to those pages with just putting in the url in the browser but that's not working. I'm new to laravel and am working on a project that was a template first so I'm having a bit of trouble finding things and putting in the correct paths.
I've tried putting the path in the web.php and my PagesController in a few different ways but nothing has worked so far.
my web.php :-
Route::get('/login.profile', 'Frontend\PageController#loginProfile');
my PagesController :-
public function loginProfile(){
$data['page_title'] = 'Profile';
return view('frontend/login.profile');
}
the path to the file :-
\Desktop\doctor\resources\views\frontend\login\profile.blade.php
Try to define the route like this:
Route::get('/login/profile', 'Frontend\PageController#loginProfile'); // I removed the dot from the url
and the controller method like this:
public function loginProfile(){
$data['page_title'] = 'Profile';
return view('frontend.login.profile', $data);
// also view('frontend.login.profile')->withData($data)
// and view('frontend.login.profile')->with(['data' => $data]) should work
// You will have a $data array available in the template
}
the path to the controller should be app/Http/Controllers/Frontend/PageController.php and the path to the view should be resources/views/frontend/login/profile.php.
When pointing to files, many Laravel method replace dots with slashes. That's a feature that's there to allow you to navigate/access stuff in a more "object-oriented" style, i would say. Let me know if it works.
This is my controller directery structure
controllers
-----user(folder)
----------User_reg.php //My Controller inside user folder.
-----index
-----Welcome.php
I can access User_reg.php like this:
http://localhost/coin_system/user/user_reg
Now I want to eliminate user from this URL so I added the route
$route['user_reg/(:any)'] = 'user/user_reg/$1';
But it show the error: 404 Page Not Found
I want to access it like this
http://localhost/coin_system/user_reg
How can i access the controller inside the directory in controller?
I have tried to solve using this SO question but it did't help.
I am using Codeigniter latest version. 3.1.5
You have missed function
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['user_reg'] = 'user/user_reg/index';
$route['user_reg/(:any)'] = 'user/user_reg/index/$1';
Or You can have different function
$route['user_reg'] = 'user/user_reg/somefunction'
$route['user_reg/(:any)'] = 'user/user_reg/somefunction/$1';
Also try with index.php in url
http://localhost/coin_system/index.php/user_reg
When you add route with :any it also find your calling method after controller. But for index(which is default) its not compulsory to all index method So you need to specify route for it also. So just need to add one more route
$route['user_reg'] = 'user/user_reg'; // add this to route file
$route['user_reg/(:any)'] = 'user/user_reg/$1';
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";
I wrote a controller named Admin with method index and placed as /application/controllers/admin.php for URL http://localhost/admin.
Then I wrote another controller named Account inside /application/controllers/admin/ folder, with a method login for URL http://localhost/admin/account/login.
But the problem is when I visit /admin/account/login I got a 404. I don't know why? Or how can I define this 2 path in my controllers?
I also faced the same problem but for only solution was to either rename the subfolder name or rename the outside controller name.
Try put admin/account/login before admin, something like this:
$route ['admin/account/login'] = 'admin/account/login';
$route ['admin'] = 'admin/index';
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.