Yii FrameWork Controller name is added int he url - yii

I am working on a web application that is using Yii framework. the website default URL and the page which is pre-defined having the following URL
http://example.com (base url)
http://example.com/contact (page url)
However, I have a problem with the custom page they are shown with the following URL
http://example.com/yes-page-name
I checked the file that is calling the page and found the following
<div class="nek" align="center">
<a href="<?php echo Yii::app()->createUrl("/yes-". trim($val['pagname_slug']))?>">
<img class="mylogo"src="<?php echo FunctionsV3::getmyCompanyLogo($company_id);?>">
</a>
</div>
config.php setting is as followed
'urlManager'=>array(
'class' => 'UrlManager',
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'' => 'web/index',
'<action:('.$patern.')>' => 'web/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
I would like to remove yes- from the url as it is not user friendly. Any help on this issue is really appreciated.

Related

Yii2 subdomain URL routing

I have two instances. The first one is actually my WordPress page and the second one is for the app. I've created an A record that points to the IP address where the instance for the app is. Let's say it's example.com
When I open app.example.com everything looks fine except links.
This is how I format the link:
<a href="<?php echo Url::to(['site/xyz/']);>xyzy/a>
However, my links are not formatted very well.
Do you know what can be a problem?
I've found this post: Yii2 - subdomain routing but I couldn't find it useful.
Maybe I need to add Virtual Host or..?
UPDATE:
My UrlManager rules:
'rules' => [
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'site',
'logout' => 'site/logout',
'sign-up' => 'site/signup',
]
Also, the links are is as the following:
<a class=" "href="//import/">anchor</a>
Thanks everyone
Try to add some more rules like this:
'rules' => [
'app.example.com/<controller:\w+>/<id:\d+>' => '<controller>/view',
'app.example.com/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
//'app.example.com/<controller:\w+>' => '<controller>/index',
'app.example.com/<action:\w+>' => 'site/<action>',
],

How can you add back authentication to Laravel 5.1?

Laravel 5 came out with a nice Auth scaffolding, which included all of the routes/controllers/views for registering and authenticating a users. But I started using Laravel 5.1 recently, and noticed authentication is no longer built in. How can I add it back?
Laravel does already have documentation on building authentication into your Laravel 5.1 app. However, I'll go through this in a bit more detail...
Installing Laravel
First, make sure you have a fresh install of Laravel. Here is my tutorial on Installing Laravel 5.1 on OSX with MAMP.
Add Twitter Bootstrap
After downloading bootstrap add the bootstrap.css file into the public/css directory. (you may have to create the css directory.
Also copy over bootstrap's fonts directory into your app's public directory.
Add Authentication Routes
Add the following routes to the app/Http/routes.php file.
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
// Password reset link request routes...
Route::get('password/email', 'Auth\PasswordController#getEmail');
Route::post('password/email', 'Auth\PasswordController#postEmail');
// Password reset routes...
Route::get('password/reset/{token}', 'Auth\PasswordController#getReset');
Route::post('password/reset', 'Auth\PasswordController#postReset');
Add Authentication Views
First let's create a blade template to use for all of our other views. We can do that by creating a resources/views/auth/app.blade.php file. And copy/paste the code shown here: https://github.com/laravel/laravel/blob/5.0/resources/views/app.blade.php
Create a new resources/views/auth directory. Within that directory, create the following files.
login.blade.php (https://github.com/laravel/laravel/blob/5.0/resources/views/auth/login.blade.php)
password.blade.php (https://github.com/laravel/laravel/blob/5.0/resources/views/auth/password.blade.php)
register.blade.php (https://github.com/laravel/laravel/blob/5.0/resources/views/auth/register.blade.php)
reset.blade.php (https://github.com/laravel/laravel/blob/5.0/resources/views/auth/reset.blade.php)
For the "forgot password" email, create a resources/views/emails directory, and place the following file into it.
password.blade.php (https://github.com/laravel/laravel/blob/5.0/resources/views/emails/password.blade.php)
Create Database & Tables
To make it so that we can actually register a new user and login, we'll have to create the proper database tables. Fortunately, this is already available through migrations.
First, create a new database table, and define it's connection in the .env file.
DB_HOST=localhost
DB_DATABASE=name
DB_USERNAME=root
DB_PASSWORD=xxxxxxx
The trigger the migration with the following command:
php artisan migrate
Since I'm using MAMP, I got this error when trying to migrate.
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
Solution was to add the unix_socket key with a value of the path that the mysql.sock resides in MAMP.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
],
Setup SMTP Server
Laravel 5.1 defaults to mailtrap.io. First time I gave this a try, and it's actually quite easy! First step is to setup mailtrap.io account.
Update .env file with SMTP settings (provided after signing up)
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=xxxxxx
MAIL_PASSWORD=xxxxxxx
MAIL_ENCRYPTION=null
Update from address in config/mail.php file.
'from' => ['address' => 'noreply#test.com', 'name' => 'test'],
Create Dashboard
Add dashboard routes
Route::get('dashboard', 'Dash\DashboardController#home');
Add dashboard controllers to app/Http/Controllers/Dash/DashboardController.php
<?php
namespace App\Http\Dash\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class DashboardController extends Controller
{
public function home(Request $request)
{
return view('dashboard/home');
}
}
Note the use of use App\Http\Controllers\Controller;. This is important since were using a different namespace for our dashboard.
And the view at resources/views/dashboard/home.blade.php:
#extends('app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
You are logged in!
</div>
</div>
</div>
</div>
</div>
#endsection
Update login redirects:
Update app/Http/Middleware/RedirectIfAuthenticated
with:
return redirect('/dashboard');
Add to Auth/PasswordController.php and Auth/AuthController.php files.
protected $redirectTo = '/dashboard';
Authenticate Dashboard
To restrict access to the dashboard to only those that are logged in, we can ddd the following to the Dashboard controller
public function __construct()
{
$this->middleware('auth');
}
Aside from the docs as pointed by Marty Thomas you can also try to use this package for auth scaffolding.

Can we implement different login url for sessiontime out and accesscontrol in Yii?

Is it possible to impliment 2 login url in Yii.
that is if a sessiontimeout occurs user should be redirected to site/login.
but when an unauthenticated user tries to access the url he should be redirected to site/loginaccount
Is it possible?
this is the code in the config file for session timeout and initializing login url
'components'=>array(
'user' => array(
'class' => 'WebUser',
'loginUrl' => array('site/loginaccount'),
'allowAutoLogin' => true,
),
'session' => array(
'class'=>'CDbHttpSession',
'timeout'=>$params['session_timeout'],
'autoStart'=>true,
),
This is code in my controller, which redirects to site/loginaccount if user is Guest
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('Createdeal'),
'users'=>array('*')
),
A hacky way to do this would be to put something like this in your view (or your template):
<head>
<?php if (!Yii::app()->user->isGuest) {?>
<meta http-equiv="refresh" content="<?php echo Yii::app()->params['session_timeout'];?>;url=http://example.com"/>
<?php }?>
</head>
Obviously the URL could be generated using createUrl(). You could do something similar via javascript.
Obviously all the usual disclaimers about using meta refersh and javascript redirects would apply . . . .

Yii url rules for main page

'urlManager'=>array(
'class'=>'application.components.UrlManager',
'urlSuffix'=>'/',
'baseUrl'=>'',
'showScriptName'=>false,
'urlFormat'=>'path',
'rules'=>array(
'<language:\w{2}>' => 'page/index',
'' => 'page/index',
'<language:\w{2}>/page/<alias:.*>' => 'pages/read',
)
link "/en/page/index" works fine
links "/" and "/en" returns the error "Unable to resolve the request" page / index ".
what is wrong with the rules
'<language:\w{2}>' => 'page/index'
'' => 'page/index',
?
UPD:
pagesController has an action:
public function actionRead($alias){
//some php code...
if($model==null)
{
throw new CHttpException(404,'page not found...');
}else
{
$this->render('read',array('model'=>(object)$model));
}
}
Your rules that don't work are redirecting to page/index, meaning that they're going to try and access PageController.php, and within that controller, they're going to try an access actionIndex. It doesn't sound like you have either a controller PageController.php, much less an actionIndex within that controller.
You need to fix the targets of those rules to include valid controller/action combinations.

Yii urlManager parameter

I am trying to use the following url: (With the htaccess applied)
/user/{username}
to redirect to something like:
/user/view/{username}
I have no clue what the rule should be...
My current urlManager config is:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
)
),
I'm also going to want some overrides such as (That go to the regular action)
/user/settings
/user/edit
etc... how would I add these?
Figured it out on my own!
'user/settings' => 'user/settings',
'user/<action>/*' => 'user/view/user/<action>',