Zend Framework - how to rewrite url to seo friendly url - apache

I got website on Zend Framework (im total noob in Zend). For example I would like to make one URL "somewebsite.com/test/about" to look like this "somewebsite.com/for-fun-link". How do i achieve this in Zend ? Im newbie in Zend and Apache server. Where i do rewrites in Zend ? I want static URL somewebsite.com/page/about rewrite to somewebsite.com/about-product
And last question: where do i usually create rewrites ? its depends of sever/technology ?

In your bootstrap, you'll need to configure some "routes". So, if your bootstrap ends something like this:
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
you can just add in some route definitions like this:
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute( 'mylovelyroute',
new Zend_Controller_Router_Route_Static( 'for-fun-link',
array( 'controller' => 'test', 'action' => 'about' )
)
);
$router->addRoute( 'myotherroute',
new Zend_Controller_Router_Route_Static( 'about-product',
array( 'controller' => 'page', 'action' => 'about' )
)
);
$router->addRoute( 'justonemore',
new Zend_Controller_Router_Route_Static( 'another/longer/path',
array( 'controller' => 'mycontroller',
'action' => 'myaction',
'someparameter' => 'foo'
)
)
);
$frontController->dispatch();
The first parameter of addRoute() is just a unique name. Zend_Controller_Router_Route_Static takes the path you'd like to capture, and then an array of parameters, including a controller and action (and module if it applies).
If you wanted to add complexity, you could load the routes out of a database, or start using more dynamic routes. The docs here are a useful next step: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.standard

Related

YII2 UrlManager wrong route

On my site I have this urlManager rule:
'city/<id:\d+>-<alias:\S*>' => 'city/view',
On page of module "user", for example this https://example.com/user/profile, there is a link for rule
Url::to(['city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
But link becomes this https://example.com/user/city/view?id=1&alias=city_alias
What I am doing wrong?
You need to add module ID in the route as well. In the simplest case it's
'user/city/<id:\d+>-<alias:\S*>' => 'user/city/view'
If there are more than one module using similar route you can use wildcard
'<module>/city/<id:\d+>-<alias:\S*>' => '<module>/city/view'
You should simply try :
Url::to(['/city/view', 'id' => $this->id, 'alias' => $this->alias], $absolute)
Read more about creating urls.

Routing not working in phalcon framework

I have many modules and depending upon modules routes are generated dynamically. For explaining purpose I m adding route for just one module. Following are the routes for the users module.
<?php
//1
$router->add('/users',array(
'module' =>'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index'
));
//2
$router->add('/users/:params/',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index',
'params'=>1
));
//3
$router->add('/users/:action',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1
));
//4
$router->add('/users/:action/:params',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1,
'params'=>2
));
?>
Lets say the url for the user module is
http://www.example.com/admin/users/
This url matches the very first route and its working as expected. But when we navigate to next page, my url looks like
http://www.example.com/admin/users/2
now the problem is it should match 2nd route, but it matches 4th route. If I move 2nd route all the way down, the above url work, but the url
http://www.example.com/admin/users/search/1 will not work
Can anybody help me make it work?
Thanks
:params is tricky because it will match anything. So /users/:params/ matches both /users/:action and /users/:action/:params, which itself has :params in it - making it a routing mind blow.
As a general rule avoid the mind blow scenario. For example you can put :params at the end of the longest possible match (/users/:action/:params) and then rewrite the shorter routes without any :params in them.

Beautifying URLs in Yii

I have following file structure
As default the url created for accessing the module content is for example
http://127.0.0.1/tmc/user/default/viewMessage
and for other controller it comes out to be
http://127.0.0.1/tmc/user/booking/index
The problem is I want to write a rule in my urlManager so that both controllers remain accessible AND i do not see default word in url as in first example.
However if i write following rules I am able to eliminate the default word but now other controllers in same module wont work. any help in this regard is appreciated
'<module:\w+>/<action:\w+>/<id:(.*?)>' => '<module>/default/<action>/<id>',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
My current Url Manager is as follow
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'/' => 'site/index',
'login' => 'site/login',
'user' => 'user/default/',
'<view:[a-zA-Z0-9-]+>/' => 'site/page',
),
),
Follow this Link for config settings
Refer this Link
//inside protected/modules/admin/AdminModule.php
class AdminModule extends CWebModule
{
//goes to TaskController instead of DefaultController
public $defaultController = 'Task';
...
Now your Yii application will routes to the “TaskController” if you request
index.php?r=admin
//same as requesting
index.php?r=admin/task

User management hybrid auth setup

I'm trying to setup hybrid auth on the Yii user management addon, the docs are here https://github.com/thyseus/yii-user-management/blob/master/user/docs/hybridauth.txt
according to this step
Take the modules/user/vendors/index.php, rename it to 'hybridauth.php' and place it
beside your application index.php bootstrap script. This will be your hybrid auth
entry script.
for this, there is NO index.php file in modules/user/vendors/index.php but there is one in modules/user/vendors/hybridauth/index.php which i renamed to hybridauth.php and put it in http://localhost/dev/ content of the hybridauth.php are
require_once( "protected/modules/user/vendors/hybridauth/Hybrid/Auth.php" );
require_once( "protected/modules/user/vendors/hybridauth/Hybrid/Endpoint.php" );
Hybrid_Endpoint::process();
now for this
Place the hybrid auth configuration file into your application
protected/config/hybridauth.php.
i took my modules/user/vendors/hybridauth/config.php and put it in protected/config/ and renamed config.php to hybridauth.php. the content looks like this
return
array(
"base_url" => Yii::app()->createAbsoluteUrl('/').'/hybridauth.php',
"providers" => array (
...........
"Google" => array (
"enabled" => true,
"keys" => array (
"id" => "ID",
"secret" => "SECRET",
),
),
"Facebook" => array (
"enabled" => true,
"keys" => array (
"id" => "ID",
"secret" => "SECRET",
),
"scope" => "email,user_birthday" // https://developers.facebook.com/docs/reference/login/
),
),
// if you want to enable logging, set 'debug_mode' to true then provide a writable file by the web server on "debug_file"
"debug_mode" => false,
"debug_file" => "",
);
problem is when i click say on the facebook icon on my login page, it shows my main page. (index.php)
here is the link it directs too
http://localhost/dev/index.php/hybridauth.php?hauth.start=Facebook&hauth.time=1388044835
when i remove the index.php from the url
http://localhost/dev/hybridauth.php?hauth.start=Facebook&hauth.time=1388044835
i get this error
You cannot access this page directly.
any idea what i'm doing wrong? Thanks
This sound like a routing problem in your configuration:
you might want to add a route for hybridauth.php and make sure it is only accessable by GET
for example :
'components'=>array(
......
'urlManager'=>array(
'showScriptName' => false,
'urlFormat'=>'path',
'rules'=>array(
'hybridauth'=>array('user/hybridauth/index', 'verb'=>'GET', 'urlSuffix'=>'.php'),
....
),
),
),
if anyone having the same problem, here is how i solved it:
by changing the base_url to point to the actual php file:
"base_url" => Yii::app()->getBaseUrl(true).'/hybridauth.php',
If you use YUM User Managment try to import
Yii::import('application.modules.profile.models.*');
on YumUserAuthController because profile cannot be found.

Using Elfinder extension in Yii

I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget