Yii2 yii\helpers\Url helpers Url to another site - php

I am using yii\helpers\Url helpers to access my site's urls in menu. But there should be a url goes to another site, like www.anothersite.com/action.
How can I place this link via yii\helpers\Url helpers ?
Is it possible to add a url that goes other site with yii2 yii\helpers\Url helpers ?

This soultion is reusable, so give a try.
Define an alias inside application's config file.
Yii::setAlias('#another', 'http://anothersite.com/action');
Then you'll be able use it throughout entire application like,
//assuming you've already imported yii\helpers\Url
Url::to('#another');

Yup, just like this:
<?= \yii\helpers\Url::to('http://www.anothersite.com/action') ?>
But this is the same as:
<?= 'http://www.anothersite.com/action' ?>
If you want to use routes there for another website (so you can use array route with action parameters for example) you need to configure first the urlManager for the other site, otherwise it will not work.

Related

Codeigniter router

I want to change Codeigniter router
ex. cms_aboutus_cate
to
controller
--cms
--aboutus
--cate.php
is there any other efficient way to achieve using routing or any other way?
$route['^cms_aboutus_cate$'] = "cms/aboutus/cate/index";
Is important that you set the index method.
I recommended you HMVC if you want modulate your app.
You can set your url in config/routes.php file.
In your case, you can put:
$route['cms_aboutus_cate'] = 'cms/aboutus/cate';
So, the actual URL will be http://yourdomainname/cms/aboutus/cate but it will show in URL is this: http://yourdomainname/cms_aboutus_cate.
Reference:
https://www.codeigniter.com/userguide3/general/routing.html

Display dynamic URLs in CodeIgniter for seo

I'm building an online store based on CodeIgniter. I'd like URLs to look like this? What is the solution for this type of SEO friendly url.
http://example.com/[product-category]/[product-sub category]
I need this url:
example.com/women/sarees-sari
But my url is generated
example.com/Product/item/MQ==/women/sarees-sari
/Product/ is my controller,
/item/ is function name,
/MQ==/ is my product id
You can use routing to handle your request url. It's simple. For example for your case:
$route['women/sarees-sari'] = 'Product/item/MQ==';
Codeigniter has _remap function that can be called on controllers. So you can call this on core controller or main controller, and call your function that wish.
CodeIgniter has very good routing System so you can modify your url as per your requirement and linking using /application/config/routes.php file.
If you open this file first time, you will see only default controller, i.e $route['default_controller'] = 'welcome';
but you can add as many routes as you want. Like in your case for seo, you should add
$route['women-sarees-sari'] = 'Product/item/MQ=='; and this will route the user from www.example.com/women-sarees-sari to correct controller and method.

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

Specifying domain in zend view url-helper

My application has two modules Product module and Blog module. Both modules use the same DB. There are two domains: the first pointing to the Product Module (www.mainsite.com) and the second to the blog module (www.blog.mainsite.com). There will be multiple links connecting from the Product Module and Blog module.
The issue is that i am using the zend url view helper. But i was not able to specify the domain to that view helper. when i call the view helper it is always returning my current domain. I have checked the zend framework manual, but nothing was found.
Is there any other option in zend to implement the same?
In case of fully qualified URI you should go for:
$this->_redirect('http://www.blog.mainsite.com/some/thing');
When you are calling the url helper in a view it actually calls route assembler. So I assume you should define two routes for each domain and use the view helper something like that:
echo $this->url (array ('action' => 'some', 'controller' => 'thing'), 'routeName');
You can try this- http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.hostname (not tested)
You can also just concat $_SERVER['HTTP_HOST'] with the URL generated by URL view helper.
Use the ServerUrl view helper, along with the "//" notation for inheriting the current URL scheme:
echo '//' . $this->serverUrl() . $this->url(/* ... */);

How to implement keyword specific page invocation using PHP codeigniter

How to implement keyword specific page invocation using PHP codeigniter
Binary search Tree Implementation.
for e.g. in above URL keyword "binary-search-tree-implementaion" is method name or parameter for specific controller. because most of things are dynamic then how web site is going to manage all those things?
I want to implement it for my web site like this
http://example.com/search/digital-camera-price-in-india
I'm not familiar with CodeIgniter, but usually a URI structure like /search/digital-camera-price-in-india would route to the Search controller and digitalCameraPriceInIndia action.
/search/digital-camera-price-in-india
=> SearchController::digitalCameraPriceInIndiaAction()
If you want to route similar URIs (different products for example) to a catch-all method, you've have to setup custom routing.
The CodeIgniter documentation for routing is here
As per #chriso's answer you will need to set up a custom route to achieve this as by default the uri structure is /controller/action/params. So in your config/routes.php file you can add something like:
$route['search/(:any)'] =
"search/some_action";
And then use the relevant uri segment (
$this->uri->segment[1]
I think) as your search parameter.
if you set up your route like this:
$route['^search/(:any)'] = "search/my_controller_function/$1";
Then you can just write your function like this:
public function my_controller_function($search_input)
{
// your code here
}

Categories