codeigniter routes - codeigniter

How to redirect controllers if they not match pattern.
For example I have follow controllers about_us, contacts, find_us. How to do so If the request method is not in that selection to be redirected to other controller ?

For some reason it's not in the documentation for URI Routing in the User Guide. If you are using CI2, inside of your routes.php file you can use "404_override". It is one of the reserved routes.
$route['404_override'] = "error";
So when a user comes to a controller that is not one you have created or not something inside your routes.php file, it throws them into the "error" controller. Obviously you would have to make a controller called "error"

Read about routing.

Related

Yii2 how to rewrite url without controller name

I am rewriting my yii2 website urls.In my config file i added
'<category_name>-<controller>-<category_id>'=>'<controller>/index'
and in the url i just passed the parameters like
<?=Url::to(['shop/index','category_id'=>1,'category_name'=>'clothes'])?>
And my url comes like
https://example.com/clothes-shop-1
This is what i am getting. But i need something like
https://example.com/clothes-1
for that i just changed the rule like this
'<category_name>-<category_id>'=>'<controller>/index'
But that time rewriting doesn't working.How can i remove the controller name from that url
How does system know, what controller is needed to proceed URL? In first example
'<category_name>-<controller>-<category_id>'=>'<controller>/index'
There is a controller name. In second
'<category_name>-<category_id>'=>'<controller>/index'
No controller name.
So you need to tell it. Try
'<category_name>-<category_id>'=>'shop/index'

laravel prefix not working in route

Route::group(array('prefix'=>'admin','middleware' => 'auth_admin_check'), function() {
Route::any("dashboard",["as"=>"admin_login_post1","uses"=>"admin\DashboardController#index"]);
});
after login i am redirecting to the page: dashboard but my prefix : 'admin' was not added.
I think you're redirecting to a url? Do you redirect to '/dashboard'? This will not prefix it.
What you should use is
redirect(route('admin_login_post1'))
This will retrieve the url by the route's name. Laravel will find the route in a group with a prefix, so Laravel will prefix the URL.
The route() function is very usefull. When you change a url, you don't have to change it troughout your whole application. This is because the function retrieves the url from the routes file. So if you change it in the routes file, you change all the links in your application.
if your prefix "admin" is not loading in the URL, then may be there is another route declared for /dashboard. check for that
and for redirection, use
redirect()->route('admin_login_post1');

Routing problems, CodeIgniter

This might be a very silly question because I'm new to PHP frameworks. But I have got the concept of MVC framework. But this routing thing is very confusing me.
I created a controller and view for dashboard and set it as default. Then I wanted to check if the user is logged in then send him to the login page if it's not.
So, what I did in the routes is this:
$route['default_controller'] = 'dashboard/index';
$route['login'] = 'login';
But even if I add a thousand more controllers and routes, it still goes to the default i.e the dashboard. This was supposed to work http://localhost/codeigniter/login. I'm very sure I haven't put a redirect-to-dashboard code in the login page.
at first it's important to understand to routing in codeigniter.
The key for understanding is $route['url/path'] = controller/method (or "public function")
So. In your case, you routing to Login controller and index method (or index function...this it interpretation of codeigniter )
If you want to check if is user logged or not, you may try to check for examle session variable inside of called functon. But it depends on your application. But I think that is not good idea to check it in routes.php
Hope it helps you.
The default controller in routes, will be the controller that opens your index or first page.
If your first page is in controller "pages", then that is your default controller. Do not use it for your secure dashboard pages
Your first page should be the index method in the controller. It should look like this
$route['default_controller'] = "pages";
Where pages is your first page controller

How to Redirect with CodeIgniter

I have issue with page redirection with codeigniter, I want to redirect from http://mywebsite_domain/index.php/home/show/206 , to http://mywebsite_domain/index.php/ar/home/pages/show/206 . And My controller name "pages" in "home" folder
please help me....
Just you have to write some code in routes.php in application/config/routes.php.
$route['show/(:any)'] = 'ar/home/pages/show/$1';
See more at CodeIgniter - Routing
You can use codeigniter's URI routing.
refer the below link-
http://www.codeigniter.com/userguide2/general/routing.html
In that you can also use Regular Expressions to get generalized URI routing.
routes.php => /application/config/routs.php

CodeIgniter controller in subfolder

I have a controller in a subfolder. CodeIgniter is giving a 404 page not found.
The controller works fine in the root controller folder. The controller also works fine in the 1st level subfolder. The controller breaks in the 2nd level subfolder.
Why would CodeIgniter not want you to user multiple subfolders?
Example:
Works: controllers/pages/HomeController.php
Broken: controllers/pages/users/HomeController.php
My Routes are like this:
Works: $route['default_controller'] = "pages/HomeController";
Broken: $route['default_controller'] = "pages/users/HomeController";
I wrote about this before, you just need to read the CI manual, but here is a quick blog entry I did which should get you back on track:
http://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/
Example:
$route['account/manage/(:num)/(:any)'] = "account/manage/index/$1/$2";
CodeIgniter does not inherently allow for multiple controller folders. It may or may not work, but it is an undocumented quirk. Using the routes.php file you can virtualize any folder or controller structure you want, just take care to map your routes back to a controller and method in the Controllers folder.

Resources