This is a question about Yii 2. I have already done this for Yii 1.x (previous versions), but Yii 2 seems quite different.
For a static web site, I need to set my base URL to http://www.sampledomain.com
I understand that default server name can be used. However I'd like to hard code my domain name to some one place in the framework.
In my /config/web.php (config file). I have this configuration.
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
...
Currently Yii::$app->request->BaseUrl returns and empty.
If this is all about setting and getting Yii baseurl. Could anyone please help by showing me how the code above can change, so I can set the base URL to http://www.sampledomain.com
At the moment, a Yii 2 static site takes the $_SERVER['SERVER_NAME'] of the domain, so it uses the domain being visited. However if my static site has several parked domains, I want all my internal links within the website to use one domain only. Is this possible?
In params.php
add
return [
.....,
'domainName' => 'yourDomani.com',
]
and in controller you can call it by
Yii::$app->params['domainName'];
Related
I have got a Laravel website a: www.website.com and b: www.website.co.kr.
As you can see website b has a different extension 'co.kr'.
Both urls route to the same server. Most pages are the same for both websites, but some pages are different.
I want to handle this in my routing.
So for example:
www.website.com/about-us
www.website.co.kr/about-us
Shows a different page.
Right now I handle this using a group:
$co_kr_routes = function () {
Route::get('/about-us', [
'uses' => 'Frontend\Korea\PagesController#getAboutUs',
]);
};
Route::group(['domain' => 'www.website.co.kr'], $co_kr_routes);
//Default
Route::get('/about-us', [
'uses' => 'Frontend\PagesController#getAboutUs',
]);
This works, but I am not satisfied with the solution and it is causing me not being able to cache the routes 'php artisan route:cache' because of the duplicate route '/about-us'.
My question is:
Is there a nicer way to handle different extensions but same server resulting in showing a different page?
I didn't find much help googling the issue.
Right now I have to very specifically give the .co.kr domain to the group but it would be nicer if I could filter on 'co.kr' instead of the full domain.
I thought a solution would be handling this in the controller method itself, but handling this using routes sounds like the way to go. I dont think the controller should know or care if a request was made from a .com or .co.kr extension.
The CMS i'm developing using Cakephp 2.0 has two main Controllers:
Pages
Categories
I'm trying to set the route.php to have the following behavior:
If the user request a Page, the URL should be something like:
http://www.something.com/pages-alias/article-name/id/whatever
If the user address a Category, the URL should be something like:
http://www.something.com/categories-alias/category-name/id/whatever
Please notice that following categories and pages i've used "alias".
To clarify with an example, the URLs for a website of a local restaurant will be:
http://www.something.com/course/wild-boar/68/2013-07-18
Where "course" will substitute "page". And
http://www.something.com/menu/valentine-day/8/2014-01-30
Where "menu" will substitute "category".
The View should not be explicited in the URL nor the Routing rules.
Both the cases will have the view automatically choosen by the controller after some internal check (having subcategory, having only one page or more pages, and so on) so that will be overridden by the controller.
I've got some clues about the use of sort-of "alias" to build the routing rules but unfortunately the documentation was not clear enough to me about how to manage the object to create my own custom route.
So, can someone try to explain it with some example different from the ones available in the CakePhP 2.x documentation?
Thanks in advance to anyone that can be helpful.
For reference i'll paste here the links i've already read:
Routing - Cakephp 2.0 Documentation
Bakery Article from Frank (i suppose this is for the v1.3)
That is what you want probably:
Router::connect(
'/:category_alias/:category_name/:id/:whatever',
array('controller' => 'Article', 'action' => 'view'),
array('pass' => array('category_alias','category_name','id','whatever'),
'id' => '[0-9]+')
);
ofc you can delete this validator for id.. or add more validators :)
Then you can use function in ArticleController.php
public function view($category_alias, $category_name, $id, $whatever) {}
Been trying to figure this out for a few days now with no joy. Here's my setup: I have a CakePHP install in
/home/user/tools/cakephp
and a plugin at
/home/user/tools/cakephp/app/Plugin/MyPlugin
The Apache server setup is such that I've set the DocRoot to /home/user/tools, so browsing to
http://myserver.com/cakephp/my_plugin
works fine, but now my client wants to set it up so that
http://myserver.com/product-name
serves up the CakePHP plugin, and all subsequent routes are honoured. Has anyone had any experience setting something like this up? Has to be Apache, unfortunately, and can be done with a mixture of config/.htaccess (clients constraints).
Thanks
Stephen
You are saying you have to do this via the Apache config or a .htaccess file, why? A plugin can have either his own Config/routes.php or you could configure the routing in your app wide app/Config/routes.php file. We are doing the same for a plugin we are using in our application.
What we did:
In the app wide routes file (app/Config/routes.php) we set a variable we use as our base url for accessing the plugin. We set it in a variable so we can easily switch when we get collisions with other controllers or plugins, so we want to keep that flexibility by doing the following:
# set the webroot for the plugin, for ajax calls and the sake of usability
$MyPluginBase = '/product-name';
# And we have this set just in case we need it somewhere in our application
Configure::write('MyPlugin.base', $MyPluginBase);
Then we configure our custom routes:
Router::connect($MyPluginBase . '/:name', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_products',
'action' => 'products'
));
Router::connect($MyPluginBase . '/some/other/url/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_some_controller',
'action' => 'whatever'
));
Now we can access the pugin via the 'product-name' url.
But, when you simply need a controller/action wide solution for this, you could accomplish this by using the following two routes:
Router::connect($MyPluginBase . '/:controller/:action/*', array(
'plugin' => 'my_plugin'
));
Router::connect($MyPluginBase . '/*', array(
'plugin' => 'my_plugin',
'controller' => 'my_plugin_main_controller'
));
Please note that the order of Router::connect methods matters!
ps. After reading the question again, I saw you have set your DocumentRoot wrong for production. Consult the following page in the cookbook for clarification: http://book.cakephp.org/2.0/en/installation.html#production
Adding the following line to the htaccess file should work
RedirectMatch 301 cake/my_plugin(.*) /product-name$1
The above would resolve:
http://myserver.com/cakephp/my_plugin to http://myserver.com/product-name
http://myserver.com/cakephp/my_plugin/somelink to http://myserver.com/product-name/somelink
When you say "all subsequent routes are honoured", I assume you mean that http://myserver.com/product-name/foo/bar will work the same as http://myserver.com/cakephp/my_plugin/foo/bar.
If that is the case, and you have mod_aliasinstalled, all you need to do is provide an Alias directive in httpd.conf:
Alias /product-name /cakephp/my_plugin
This should be completely transparent to CakePHP; it will be unaware that this mapping is happening.
If you want to prevent direct requests to http://myserver.com/cakephp/..., you could also add an external redirect:
Redirect 301 /cakephp/my_plugin /product-name
(from http://httpd.apache.org/docs/current/mod/mod_alias.html#alias)
I'm new with Kohana and finding their documentation to be lacking (lots of incomplete writings, lots of broken links, etc.). I just want to create a route like so:
Route::set('test1', 'blah/<id>')
->defaults(array(
'controller' => 'Blah',
'action' => 'foo',
));
So if the URL is localhost/blah/8342342 it will run through this controller and action. The problem is I get a 404 error. If I change the URI in the Route::set to be blah/foo/<id> it works fine. I only want the /blah/ directory though, not 'blah/foo'. Is this possible or do you need to have both the controller and action in the URL?
Another question, does the first directory in your URI (in this case /blah) HAVE to match the controller name? For example, if the first directory in the URI is "blah/", does that mean my controller must be named "Blah.php"? From my tests it seems that this is the case, but I don't know why it would be set up that way. What if I wanted the URI "contact/" to go through controller Blah?
I have built a site on zend-framework 1.9.7. I want to make friendly url for every page which has a URL similar to this : http://mysite/search/detail/id/124
I want the friendly URL to look like: http://mysite/search/detail/ram
Where "ram" is the name of user which has id=124
I have include RewriteRule ^name/(.*)/$ guid/$1 in .htaccess file, but it doesn't work.
I suggest you to take a look at the Zend Controller Quickstart which walks through the steps of setting up the standard routing (which already provides everything for nice URLs).
If you want more detailed Info on the Routing, then I suggest to take a look at Zend_Controller_Router's Manual.
Specifically I would handle your case through a Router Route, for example:
<?php
$router = Zend_Controller_Front::getInstance()->getRouter();
$detailsRoute = new Zend_Controller_Router_Route("search/detail/:name", array(
'controller' => 'search',
'action' => 'detail'
));
$router->addRoute('searchDetail', $detailsRoute);
The part :name is a parameter which gets filled with the value ram of you desired URL, and can be retrieved with $request->getParam('name'); later on.
Controllers in ZF have the functionality to be called from custom routes. You can find the documentation here. They give you a wide variety of options to choose the kind of route you want to use. It can be pretty URLs like those in blogs or even REST endpoints.
You don't have to mess with the htaccess file for this as all calls to non-static resources are directed through index.php anyway.
have a zend plugin that works very well for this.
<?php
/** dependencies **/
require 'Zend/Loader/Autoloader.php';
require 'Zag/Filter/CharConvert.php';
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
//filter
$filter = new Zag_Filter_CharConvert(array(
'replaceWhiteSpace' => '-',
'locale' => 'en_US',
'charset'=> 'UTF-8'
));
echo $filter->filter('ééé ááá 90');//eee-aaa-90
echo $filter->filter('óóó 10aáééé');//ooo-10aaeee
the plugin is super easy to use.
hug!