User friendly urls revelsal - redirect

Lets say that I want to show http://example.com/topic/view/topicname/thenameofthetopic direclty as http://example.com/thenameofthetopic
So I have achieved that when entering http:// example.com/thenameofthetopic , it redirects you to the controller/action properly, and the website shows what I want to show, the problem is that of course, the URL changed, and I want it to maintain the format http://example.com/thenameofthetopic
Is that possible whitout touching the htaccess file? Just configuring it with yii
How should I do that?

Exactly your case is:
'components' => array(
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
"<name_of_topic:\w+>/" => 'controller/action'
),
),
)
You will get the name of topic in Yii::app()->request->getQuery('name_of_topic');
htaccess is Yii - classic, the must be present:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
I would advice you to do another way.
'components' => array(
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
"t/<name_of_topic:\w+>/" => 'controller/action'
),
),
)
So topic link will be http://www.example.com/t/name_of_topic.
This will allow you to use "a//" for articles etc.
One more thing. Read this article to find much interesting things http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Related

TYPO3 realurl problems with subdomains and a global default domain

Normaly I've no problems configuring realurl. But this time I see no glue.
I've a configuration from old relurl 1.x, working with TYPO3 6.2. The goal is, to manage one global domain and some subdomains. The subdomains should work, but the global-domain should be the default domain for link-building.
root (1) [www.domain.tld]
..subpage1 (4) [subpage1.domain.tld]
....some pages (1004)
..subpage2 (5) [subpage2.domain.tld]
....some more pages (102)
explanation: name (PID) [domainrecord]
IE: "some more pages (102)" should be accessible with subpage2.domain.tld/some-more-pages but the links in menu should be www.domain.tld/subpage2/some-more-pages
A snippet of my realurl-conf:
$rootPids = array(
'www.domain.tld' => 1,
'subpage1.domain.tld' => 4,
'subpage2.domain.tld' => 5,
);
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT'] = array(
'init' => array(
'enableCHashCache' => 1,
'appendMissingSlash' => 'ifNotFile,redirect[301]',
'enableUrlDecodeCache' => 1,
'enableUrlEncodeCache' => 1,
'postVarSet_failureMode' => '',
),
'pagePath' => array(
'rootpage_id' => $rootPids[$_SERVER['HTTP_HOST']],
),
...
If I add domainrecords to subpage1 and subpage2, these domains will prepend all the time in links - thats not what I want.
And the part "subpage2" is removed from the default-url - which is also not what I want.
I figured out, that this is not a solution for realurl, it's a htaccess-thing.
I added the following lines to my htaccess and all is fine:
RewriteCond %{HTTP_HOST} subpage1.domain.tld$ [NC]
RewriteRule ^(.*)$ https://www.domain.tld/subpage1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} subpage2.domain.tld$ [NC]
RewriteRule ^(.*)$ https://www.domain.tld/subpage2/$1 [R=301,L]
And of course the other rootPids for subpage1 and subpage2 has to be removed from realurl-conf!
Cool.
Nested domain are not supported by RealUrl 2.x.
To make them work with RealUrl you must change your setup (eg. move pages or remove domains)

How to Create a REST API for Yii2-basic-template

I wanted to create a REST API for a yii2 basic template. I followed the following link.
I created a table named users, a controller named UserController
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
?>
and in the web
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '4534',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
my file name is restapi so i tried this url http://localhost/~user/restapi/web/
all i get is a 404 page not found error. Any help would be appreciated
Rest Api is very simple to to implement in Yii2 basic app. Just follow the steps below. This code is working for me.
application structure
yourapp
+ web
+ config
+ controllers
...
+ api
+ config
+ modules
+ v1
+ controllers
.htaccess
index.php
api/index.php
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
// Use a distinct configuration for the API
$config = require(__DIR__ . '/config/api.php');
(new yii\web\Application($config))->run();
api/.htaccess
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
api/config/api.php
<?php
$db = require(__DIR__ . '/../../config/db.php');
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'name' => 'TimeTracker',
// Need to get one level up:
'basePath' => dirname(__DIR__).'/..',
'bootstrap' => ['log'],
'components' => [
'request' => [
// Enable JSON Input:
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
// Create API log in the standard log dir
// But in file 'api.log':
'logFile' => '#app/runtime/logs/api.log',
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['v1/project','v1/time']],
],
],
'db' => $db,
],
'modules' => [
'v1' => [
'class' => 'app\api\modules\v1\Module',
],
],
'params' => $params,
];
return $config;
api/modules/v1/Module.php
<?php
// Check this namespace:
namespace app\api\modules\v1;
class Module extends \yii\base\Module
{
public function init()
{
parent::init();
// ... other initialization code ...
}
}
api/modules/v1/controllers/ProjectController.php
<?php
namespace app\api\modules\v1\controllers;
use yii\rest\ActiveController;
class ProjectController extends ActiveController
{
// We are using the regular web app modules:
public $modelClass = 'app\models\Project';
}
reference
With those configurations :
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
your resources should be available within those urls :
http://localhost/~user/restapi/web/users
http://localhost/~user/restapi/web/users/1
Note: Yii will automatically pluralize controller names for use in endpoints unless you configure the yii\rest\UrlRule::$pluralize property to not do so.
Also you need to configure your server before enabling Pretty Urls by adding a .htaccess file with this content to your web folder if using apache server ( pls refer to link below if using nginx ) :
# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"
<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
This part wasn't described in the documentation of the link you provided as it was expecting that you did follow the installation & server configuration section :
http://www.yiiframework.com/doc-2.0/guide-start-installation.html#configuring-web-servers

ZF1 Remove 'public/' from url

Using Zend_Navigation, i noticed that it's add '/public/' to all links.
How to remove this addition form url?
$navigation = array(
array(
'label' => 'Home',
'title' => 'Go Home',
'module' => 'default',
'controller' => 'index',
'action' => 'index',
'route' => 'default',
'order' => -100 // make sure home is the first page
),
array(
'label' => 'Test static page!',
'route' => 'pages',
'params' => array(
'permalink' => 'test'
)
)
);
$nav = new Zend_Navigation($navigation);
The solution was simple. Edit your bootsrap.ini like this:
resources.frontController.baseUrl = "/"
Simple way I'm using:
Put index.php to the root directory.
Define APPLICATION_PATH as following:
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
Run project without public part.
in your .htaccess file you need to add a rule for that,
try following,
RewriteCond %{REQUEST_URI} !/public [NC]
RewriteRule ^(.*)$ public/$1 [L]
add these line in your .htaccess line, and see if it works..
or you can create a virtual host, which directly points to your public directory,
as folllowing,
<VirtualHost *>
ServerAdmin admin#localhost.com
DocumentRoot "C:/wamp/www/project/public"
ServerName localhost.test
ServerAlias localhost.test
<Directory "C:/wamp/www/project/public">
DirectoryIndex index.php
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
you need to follow somethings first in order to create a vhost.
OFFICIAL DOC FOR VIRTUAL HOST

zend pagination url format

i've followed the pagination tutorial from http://framework.zend.com/manual/en/zend.paginator.usage.html
I have successfully implemented pagination for my site, but i am not satisfied with the URLs output for the paging. example url for page 2:
http://www.example.com/posts/index/page/2
What i would like is to remove the index and just have http://www.example.com/posts/page/2
Why is index included while accessing this->url(in the my_pagination_control.phtml from tutorial in link)?
Is there a way to gracefully just show posts/page/2? or even just posts/2?
I feel that the previous answer is not enough, I'll give mine. First of all you can add a router in your bootstrap.php that looks like:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$Router = Zend_Controller_Front::getInstance()->getRouter();
$Route = new Zend_Controller_Router_Route(
':controller/*',
array(
'controller' => 'index',
'action' => 'index'
)
);
$Router->addRoute('paginator1', $Route);
$Route = new Zend_Controller_Router_Route(
':controller/:page/*',
array(
'controller' => 'index',
'action' => 'index',
),
array(
'page' => '[0-9]+'
)
);
$Router->addRoute('paginator2', $Route);
}
}
and then, use in your view this simple line:
echo $this->url(array('controller' => 'CONTROLLER-NAME', 'page' => 5), 'paginator1', TRUE);
echo $this->url(array('controller' => 'CONTROLLER-NAME', 'page' => 5), 'paginator2', TRUE);
In the case of 'paginator1', the url will be printed in this way:
/CONTROLLER-NAME/page/5
In the case of 'paginator2', the url will be printed in this way:
/CONTROLLER-NAME/5
Obviously where you see CONTROLLER-NAME will be the name of the controller you write.
You could do:
in your htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Link for reference: http://framework.zend.com/manual/en/zend.controller.router.html
Hope it helps

Zend custom Route not working if Index controller is not capitalized?

I'm testing a Zend project on my shared-hosting.I keep everything inside a folder 'Zend-project' not on the server public-root (cause I have there another project!).
this is the structure:
/public_root
/zend-project
/application
/configs
application.ini
/controllers
/layouts
/views
bootstrap.php
/css
/images
/javascript
/zend-library
.htaccess
index.php
I had to tweak a little the project cause I just can't change my document_root on a shared-hosting so I edited the .htaccess to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/favicon.ico$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /zend-project/index.php [NC,L]
but at the end everything seems to work fine except a single Url Route that I added to the router in my bootstrap.
edit:I tried creating a new controller 'TestController'..with a single action (called 'test')I tried to type the url with the controller lowercase (mysite.com/zend-project/test/test)and it's working!As I suspected there is something wrong with the 'index' word itself!cause any other controller works as a charme
this is bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$frontController=Zend_Controller_Front::getInstance();
$router=$frontController->getRouter();
$router->removeDefaultRoutes();
$router->setGlobalParam('lang','en');
$router->addRoute(
'lang',
new Zend_Controller_Router_Route(':lang/:controller/:action',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
//the following route is not working remotely.
//Is working on local environment
$router->addRoute(
'langController',
new Zend_Controller_Router_Route(':controller/:action',
array(
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langIndex',
new Zend_Controller_Router_Route(':lang',
array('lang'=>':lang',
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
$router->addRoute(
'langNothing',
new Zend_Controller_Router_Route('',
array(
'module'=>'default',
'controller'=>'index',
'action'=>'index'
)
)
);
}
}
I tried to type the following urls (based on the custom routes I created)and everithing seems to work:
//this points to the pair controller/action -> index/index
mysite.com/zend-project/
//this points to the pair controller/action ->index/index in english lang
mysite.com/zend-project/en
//this points to the pair controller/action ->index/rooms in english lang
mysite.com/zend-project/en/index/rooms
But whenever I type:
mysite.com/zend-project/index/index
I receive the following message:
Not Found.The requested URL /zend-project/index/index was not found on this server.
It looks like the request doesn't reach the index.php file ..maybe an .htaccess problem??or what
thanks
edited
Found out that by typing the broken route's controller with capital letter it works :
//this works.Controller has capital letter
mysite.com/zend-project/Index/index
//this do not work.Controller has not capital letter
mysite.com/zend-project/index/index
why??
(by the way I'm on linux server..)
Remove this from httpd.conf
Options Includes ExecCGI MultiViews FollowSymLinks Indexes