1
votes

I am new to yii2. I have succesfully extracted the advanced yii2 zip template. have set up the db and the .htaccess file.

On creating a new controller , model and action in the controller and navigating to the location. Eg: http://localhost/advanced/frontend/web/mysetofusers/signin

I face three issues. 1. it says 404 error. 2. says UnKnownClassException - ie Unable to find mysetofusersController in file frontendControllers\mysetofusersController.php namespace missing?? even though the file is there in the folder and exact name space is use. 3. It always redirects to site/index page rather than showing up the mysetofusers/signin. This time tehre is no exception in the Yii debugger.

a=Also note i have tried to create models using both gii and extended class of Model.

Nothing seems to work. Pls help me out. I am stuck and not able to move ahead with the work.

Thanks:)

1
is it necessary to start with the advanced template ? it is highly recommended to start with the basic one, much easier to setup and easier to learn. Then when your app is almost done, moving your models, controllers and components to an advanced structure will be matter of copy/past and some extra configs that you'll understand their need by so far. - Salem Ouerdani
Its not. proj is neither small and since advanced had all the directory structure for backend/frontend /common and asi was planning to create REST API thot its was ideal to use advanced template. haven't tried with basic yet. will do so. However if anyone has already had this prob please let me know the solution for it. Thanks! - DpGp
Well what I'm saying is that it will not be hard to move your files from a basic to an advanced template at a further step. If you consider going with a basic one then this link may help you. That is how I restructure the basic template to implement a REST API within an api folder by keeping my web folder untouched, each have its own controllers & configs and both shares the same app models. - Salem Ouerdani
sure il try that then. Thanks salem:) - DpGp
Hi Salem, On using basic template also i see 404 error. is it something related to urlManager. Eg: MyListUsersController I have followed the link you have shared. And my urlManager Says this 'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false,'rules' => [['class' => 'yii\rest\UrlRule', 'controller' => 'v1/MyListUser'], 'POST api/web/v1/<controller:\w+>s' => '<controller>/create', - DpGp

1 Answers

1
votes

404 errors in Yii 2.0 usually came from wrong urlManager configs or Server configs.

Before creating the api module be sure that your REST app works after following the official docs. If not then it is probably a Server config issue.

In order for REST to work, pretty urls must be enabled by configuring the .htaccess file ( if using apache server ) like so :

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

Then you can go for restructuring your app.

This is a NOT YET COMPLETED but a working repo where you'll find the configs I used for the api folder and the onces I used for the web folder + the two .htaccess files inside each of the two folders.

I hope it helps.