How can I make Access to folders in Yii? - yii

In many folders, I make access with .htaccess file like this:
RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
Also I use Rbac. How can I make access to folders only to admin?

Yii::app()->user->checkAccess('admin')
public function accessRules()
{
return array(
array('allow',
'actions'=>array('admin'),
'roles'=>array('staff', 'devel'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

Related

Create route at user conveniance

I need to create a small application with a homepage where the user can fill a form with a unique field. The field will be used as the name of a room, and the user redirected to this page. Every other user who enter the same room name will be redirected to the same page, and will be able to chat with users in this room.
My problem is that I can't find a way with silex 2 to make this work.
I created the homepage, and it seems to work. I have a form and can enter the name of the room :
$app->match(
'/',
function (Request $request) use ($app) {
$data = array(
'room' => 'Name of the room',
);
$form = $app['form.factory']->createBuilder(FormType::class, $data)
->add('room', TextType::class)
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$roomName = $data['room'];
//return $app->redirect('/room/' . $roomName);
$subRequest = Request::create($app['url_generator']->generate('/room/' . $roomName), 'GET');
return $app->handle($subRequest, \Symfony\Component\HttpKernel\HttpKernelInterface::SUB_REQUEST, false);
}
return $app['twig']->render('base.twig', array('form' => $form->createView()));
}
)->bind('home');
As you can see, I tried with redirect, and with a subrequest.
But both case don't work, as I receive a 404.
The code wich is suppose to catch the room is :
$app->match(
'/room/{name}',
function ($name) use ($app) {
return $app['twig']->render('room.twig', array('name' => $name));
}
)->bind('room');
Does anyone have an idea on how I could make this work ?
Thank you for reading. Any help will be appreciated.
I finally found the solution.
I had to add a .htaccess file in my web folder. The important part of this file was this, for anyone having the same problem:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

url manager show 404 error on contact page

I'm trying to make my url look like this mysite.com/contact unfortunately it shows a 404 error on the contact page. the login page works tho. the file is located in protected/view/site/contact.php
'urlManager'=>array(
'urlFormat'=>'path',
//'urlSuffix'=>'.html',
'rules'=>array(
'<view:(about|terms|faq|privacy)>' => 'site/page',
'<action:(contact|login)>' => 'site/<action>',
'<action:(registration|create)>' => 'user/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
'showScriptName' => false,
),
make sure you have this in your SiteController.php
/**
* Displays the contact page
*/
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
$headers="From: $name <{$model->email}>\r\n".
"Reply-To: {$model->email}\r\n".
"MIME-Version: 1.0\r\n".
"Content-Type: text/plain; charset=UTF-8";
mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
I render mine like so
'contact' => array('site/contact'),
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
Create .htaccess in your project folder with above code

How to switch locales/languages for different users

I have designed a web application, It works for two different users say user1 and user2, and both of the users need the view in different languages.
I have studied about yii:t() but by that method we have to define language in main.config, which set the same language for both users.
How can I translate my view in different languages for both users?
I hope this can help you:
you need to edit urlmanager.php in your components, if there is no file, you need to create one.
Check this url: Multilingual
Thanks.
Put this in your SiteController.php:
public function actionChangeLocale($locale) {
// (OPTIONAL) if is registered user (not guest), save preferred locale in database
if (!Yii::app()->user->isGuest) {
// Update user settings
$uid = Yii::app()->user->id;
User::model()->updateByPk($uid, array('locale' => $locale));
}
// change locale
Yii::app()->user->setState('_locale', $locale);
// redirect to previous page, in the new locale
if(isset($_SERVER["HTTP_REFERER"]))
$referrer = $_SERVER["HTTP_REFERER"];
else
$referrer = Yii::app()->getBaseUrl(true) . '/';
$this->redirect($referrer);
}
Edit your main.php config url manager rules:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'lang/<id:\w+>' => 'site/changeLocale',
To change locale, create a link to point user to desired locale:
http://www.mysite.com/myapp/lang/en
http://www.mysite.com/myapp/lang/zh
http://www.mysite.com/myapp/lang/ja
http://www.mysite.com/myapp/lang/in
...
If you saved the logged-in user's preferred locale in database, add this to SiteController.php Login action:
$uid = Yii::app()->user->id;
$user = User::model()->findbypk($uid);
$userLocale = isset($user->locale) ? $model->locale : Yii::app()->language;
Yii::app()->user->setState('_locale', $userLocale);
Above usage is for those using htaccess rewrite. Make sure base .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L] # Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Related Articles:
http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n
Related Modules:
http://www.yiiframework.com/extension/ei18n/

HTTPS and Zend Framework

I am trying to convert a functional ZF application to use SSL. The certificate is valid and works, but I am having trouble configuring the application.
Here's what's in .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
SetEnv APPLICATION_ENV development
IndexController is really simple:
class IndexController extends Zend_Controller_Action
{
public function indexAction() {
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->_helper->redirector('index', 'dash');
} else {
$this->_helper->redirector('index', 'auth');
}
}
}
When I browse to the site without specifying https or port, it accurately routes me to https://app-url.com, but then tries to redirect to https://app-url.com/auth and returns a 403. What am I missing?
Assuming that 'index' is the name for the controller and 'dash' the name for the action, your parameters for the Redirector Helper1 are in the wrong order.
The first parameter is the action, the second the controller. So the correct method call would be
$this->_helper->redirector('dash', 'index');
This will redirect you to the URL /index/dash if no particular routes are set.
In the case that 'dash' is indeed the name of the controller and 'index' the name of the action, simply add a new controller named DashController which contains an indexAction() method and the redirect should work.
1) If you call the helper via $this->_helper->name this will call the direct() method, which in the redirector helper calls the method gotoSimple($action, $controller = null, $module = null, array $params = array())

rewrite rule for codeigniter

this is my controller in CI
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
}
function bil($model='')
{ }
I want to do a rewrite so that
http://example.com/index.php/welcome/bil/model
becomes
http://example.com/model
in my htaccess I have
RewriteBase /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/welcome/$1 [L]
#RewriteRule ^(.*)$ /index.php/welcome/bil/$1 [L]
I thought it should be as easy as removing the /index.php/welcome/ part
but when I uncomment the last line it get 500 internal server error
You'll want to use mod_rewrite to remove your index.php file like you have above, but use CodeIgniter's routing features to reroute example.com/model to example.com/welcome/bil/model.
In your routes.php configuration file, you can then define a new route like this:
// a URL with anything after example.com
// will get remapped to the "welcome" class and the "bil" function,
// passing the match as a variable
$route['(:any)'] = "welcome/bil/$1";
So then, typing example.com/abc123 would be equivalent to example.com/welcome/bil/abc123.
Note that only characters permitted by $config['permitted_uri_chars'] (which is located in your config.php file) are allowed in a URL.
Hope that helps!