Yii Controller As Variable In URL - php

Using the PHP framework Yii I want to have a url like url.com/xyz where xyz is an argument I want to pass to a specific controller (I wont know the value of this argument as it is dynamically generated). Is this possible? To my understanding Yii will see xyz as a controller and I am not sure how to change this without destroying the functionality of the rest of the site (url.com/controller/arg/etc)
Help? :)

Obviously you can do this. For example the if you set a url rule
'<view:\w+>'=>'site/page',
will treat anything of url.com/xyz to the view parameter of the action page of the controller site.
THE PROBLEM IS THAT the for every other routes you have to specify an action , or in other words defaultAction of any other controller has no effect.
ie the url url.com/site WITHOUT the above rule will redirect to the site/index and to site/page with view="index" WITH the above rule

This is explained in the "URL management" chapter of the guide: http://www.yiiframework.com/doc/guide/1.1/en/topics.url
A rule like the following would route a request to example.com/foo to the action myaction in mycontroller, giving the value foo to the parameter name.
'<name>' => 'mycontroller/myaction'
This rule should be the last one in the rule list. Otherwise all requests would be passed to myaction, breaking existing parts of the site.

Related

Routing in CodeIgniter for (:any)

I'm trying to make my CodeIgniter application work similarly to WordPress.
I want to be able to make these kind of URLs:
http://www.example.com/my-post-example
http://www.example.com/new-headline-here
http://www.example.com/i-love-stackoverflow
My routing:
$route['(:any)'] = "core/index/$1";
Which will call my Core controller and pass the page name into the index function.
I then lookup in my database for the page name and display the page to the user. So far so good.
However, there will be times when I want to call another controller. For example:
http://www.example.com/admin/edit_page/3
http://www.example.com/admin/settings
Now I assume my route will just grab all these rules and send them into my Core controller. Is there a way to make an exception for certain pages? Or is it a good idea to do this check inside my Core controller.
For example,
if ($page not in DB) {
// Call controller/method
}
This seems a little redundant since I just want CodeIgniter to handle this.
The routing rule you using it is OK for your purpose.
If you use http://www.example.com/admin/edit_page/3 this link it will send you admin controller and edit_page method.It will not use routes any rule.
However you will get one problem if your link looks like this
http://www.example.com/my-post-example/test
It will try to go my-post-example controller and test method.
Again http://www.example.com/admin will use routes any rule, means it will redirect your to core controller instead of admin/index. In that case your url should be http://www.example.com/admin/index
Finally If you call your other link with controller/method name it will be OK using your any rule

Remove Controller from every url in codeigniter

I have searched everywhere but i can't find appropriate result. I want to remove controller_name from every url of the site. My codeigniter is installed in subfolder of domain.
For Example:
www.site_name.com/subfolder/controller_name/any_method_name
I have only one controller from which i am calling all the methods.
I have learned that i have to do some changes in routes but i think that is only for one url. so, how to remove for every url.
Not sure if this works but you could try adding this in your application\config\routes.php:
$route['([a-z]+)'] = 'controller_name/$1';
The documentation https://www.codeigniter.com/userguide3/general/routing.html#regular-expressions
Here, I am answering my own question. I have solved this problem by following solution.
Suppose, my controller name is users, then the answer is:
$route['^(:any)(/:any)?$'] = "users/$0";
This removes controller name from every url of the website.
Codeigniter support two types of routing rules
1)Wildcards
2)Regular Expressions
I prefer Wildcards
in routes just place this one
$route['login/(:any)'] = "v1/login";
A URL with "login" as the first segment, and anything in the second will be remapped to the "v1" class and the "login" method.
means your change www.abc.com/login instead of www.abc.com/v1/login
check it once routing in codeigniter here https://www.codeigniter.com/userguide3/general/routing.html .......

Create a Dynamic URL and Redirect to it in Laravel

So basically this is what I need. I have a router definition like this.
Route::get('view/{postId}', 'PostController#view');
Above router definition will get triggered if the url we request is www.domain.com/view/4. But what I want is I want to appear this url like www.domain.com/[category-of-post]/[titile-of-post] (Ex : www.domain.com/music/easy-chords-in-guitar).
In PostController, I will get the post using the id passed and thus can generate the url what I need. Here is the problem begins. As you can see, I need to redirect to a dynamic url which will look differently for each post. I want to redirect to this dynamic urls but these urls are not defined inside routes.php.
Is this possible in Laravel ?
In short, what I need is, I want to update Illuminate\Routing\RouteCollection::$route array with my dynamically generated url value with corresponding controller action in run time before I am invoking Redirect::to('someurl')
If you need further clarification, I will do it for sure. Please give me your suggestions.
it is simpler than you are thinking.
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
This should be the last route defined in your route list. Any other route should go upper than this one.
this will match www.domain.com/music/easy-chords-in-guitar
rest routes define as you want.
e.g.
Route::get('/',['uses' => 'FooController#home']);
Route::get('about',['uses' => 'FooController#about']);
Route::get('contact',['uses' => 'FooController#contact']);
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
route :
Route::get('action/{slug}', 'HomeController#actionredeemvoucher')->name('home.actionredeemvoucher');
Function in controller:
public function actionredeemvoucher($slug)
{
print_r($slug);
}

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Codeigniter url path

This is the most simple way I can ask this question as I have not fully understood what's going on, or what I am not doing right.
I'm having trouble with the url.
http://localhost/index.php/user is the same as http://localhost/
but
http://localhost/index.php/user/something is not the same as http://localhost/something
How do I make http://localhost/something work?
Does it have to be http://localhost/user/something, how do I make that work?
You need to understand how CodeIgniter's URLs work.
An URL consists of some segments. http://localhost/index.php/user/something/thing In this example user, something and thing are segments of the URL.
Segments of the URL indicate which controller and which method of that controller will run. http://localhost/index.php/user/something/thing In this example the method something from user controller is called and thing is passed to that method as a parameter.
The first segment of the URL indicates the controller.
The second segment of the URL indicates the method of that controller.
The following segments are sent to that method as parameters.
But there are some defaults.
If your URL is http://localhost/index.php/something, you have something specified as the controller, but because you have not specified any method, the default method which is index is called. So the above URL is the same as http://localhost/index.php/something/index
If your URL is http://localhost/index.php/, you don't have any segments specified (no controller and no method). So the default controller which is specified in application\config\routes.php is the loaded controller. Which method of that controller will be called? Of course the index method.
--You can set the default controller by changing $route['default_controller'] = "site"; to what ever fits your application in application\config\routes.php's file.
If you want http://localhost/user/something to be the same as http://localhost/index.php/user/something, you have to create custom routes for your application. More info on that here.
http://localhost/something indicates that you are calling the index method of the Something controller class
http://localhost/user/something indicates that you are calling the something method in the User controller class.
Does that make sense?
In order to make http://localhost/something work, you need a controller called something with an index method. This would be the same as accessing http://localhost/something/index.
Alternatively, http://localhost/user/something implies that you have a user controller with a method called something.
Does that help at all?
To remove index.php from your URL you have to use the mod_rewrite method described here
Then to remove the controller name (user) from the url, you need to use routes
In your case, you would add $route['^(something|something_else|etc)(/:any)?$'] = "user/$0"; to your routes.php file

Categories