I'm new in php coding and I'm using Yii2 framework
I'm trying to make a simple project as follow:
I added a .php file named "PostController.php" in the backend/controllers and I wrote this codes in this file:
<?php
namespace backend\controllers;
use yii\web\Controller;
class PostController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
?>
and I created a file in beckend/views named "post" and in this folder I created a.php file named index.php
then I just wrote one line in index.php for test as bellow:
<h1>Hello World</h1>
Now I want to see this index file (Hello World) in my browser. Which url I should enter in my browser to see that? I tried the bellow url and it didn't worked!:
projectname.loc/index.php?r=post/index
if pretty url is true then you have to access the controller like
localhost/projectname/backend/web/index.php/controller/action
if pretty url is false then you have to access the controller like
localhost/projectname/backend/web/index.php?r=controller/action
if you project is in your local machine, if its on your server then you have to write server name instead of localhost
You should go to projectname.loc/post (or projectname.loc/post/index), to be in this action. before that you have to turn on the "prettyUrl" in your urlManager
If you use basic tamplate from yii2 your namespace must be app/backend/controllers and you should go to projectname.loc/index.php?r=backend/post/index
"backend" it will be a path to your controller, not a module
Related
I am building a web server locally using CodeIgniter and am trying to add a new page to my website. However, after creating the Controller and View files for the page I cannot access the new page at (what I think is) the desired address.
This is on a Pi running PHP 5 and Apache 2 with CodeIgniter 2.2.6. I have been able to access other pages to work through just writing the Controller and view files, but I can not access this one.
Controller page code (file name: Display_Live_State.php)
<?php
class Display_Live_State extends CI_Controller {
public function __construct()
{
//Construct page
parent::__construct();
$this->load->helper('url_helper');
}
public static function view()
{
$this->load->view('Display_Live_State_view', $data);
}
}
?>
View page code (file name: Display_Live_State_view.php)
<body>
<div>This is a test</div>
</body>
I expect to find a page with the test sentence on it at path localhost/index.php/Display_Live_State/view as I have created other pages with similar filepaths (except for the name of the controller) and have been able to access them.
If someone could enlighten me as to where CodeIgniter 'puts' the website when created, or show me what I did wrong / where the page is if it exists, I would be very grateful. Thanks.
Change Display_Live_State to Display_live_state both in the class declaration e.g. class Display_live_state and in the filename application/controllers/Display_live_state.php.
Set your base_url if you haven't already to http://localhost.
You should be able to access your controller via the url: localhost/index.php/Display_Live_State/view
Just started learning laravel I tried to create a route for my view but when I load on the web browser it says Sorry, the page you are looking for could not be found. Can any one help me out in codeigniter it was so simple just create a controller and view and we can see on the web broswer I found laravel to be difficult then codigniter is that true?? can anyone define me how is the mvc structure for laravel5 as i found tutorials but they are of old laravel and files and structure are almost change so I got confused any suggestions please
routes.php
Route::get('main', 'Main#index');
Main.php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class main extends Contoller {
public function _construct() {
$this->middleware('guest');
}
public function index() {
return "Hello World from controller";
}
}
if you are running laravel project locally, it can run through its own server. you dont need apache server form wamp or xampp,,but you will need their mysql database. So start only that if you require database.
Now go to command prompt, navigate to the directory where your project is stored eg cd c:/wamp/www/yourprojet and then type following command
php artisan serve
it will start on port 8000 by default. and you can now access your project at 'http://localhost:8000/'
and you can access your view at 'http://localhost:8000/main'
Also you can find laravel tutorials and other at laracast
Try to change the class name to Main (now is main, in lowecase)
I learnt Laravel by their awesome tutorial: https://laravel.com/docs/4.2/quick
make a view in resources->views, something like my-view.blade.php
Then return view('my-view');
That my-view.blade.php can have whatever HTML you want in it
Go to your resources/views folder create file with filename.blade.php.
Now in your routes.php:
Route::get('main', 'Main#index');
And in your controller add this function:
public function index() {
return view('filename');
}
I have integrated Jquery file uploader plugin via https://blueimp.github.io/jQuery-File-Upload/. Its working perfectly. I have placed the server folder of the plugin in /public of laravel. Now i have to use the session variable to check the maximum file size available for particular user, authenticating user etc. There are two files in server/php if you can see. I am trying to use like this, which is not affecting anything. if (session_id() == '') {
#session_start();
/* or Session:start(); */
}
$uid=$_SESSION['id'];
print $uid;
I also tried the same thing in the userController.php's constructor using $this->uid=$_SESSION['id']; Nothing seems working . I am not understanding how to access sessions in this. Or any other way via which i can try?
Thank You!
12-05-16-vh
Implimenting Jquery file uploader to laravel
https://blueimp.github.io/jQuery-File-Upload/
Download the zipfile from above link
put them in public folder(I have placed in public/asset/vender/)
The server/php is the one where the important codes(index.php, uploadhandler.php) where kept.
If use that, the sessions cannot be accessed.
Best way is to make customization to the code
update main.js in /js folder of the plugin byChanging this line
url: 'server/php/'
to some controller route
eg: url: 'file_uploader'
then in the routes.php, give connection to a controller class for file_uploader'
Route::post('file_uploader', 'userController#file_uploader');
Route::get('file_uploader', 'userController#file_uploader');
Route::DELETE('file_uploader', 'userController#file_uploader');
In the userController, create a function, which can then initate the instance of the uploadHandler class(which we are going to place in seperate controller)
add this in beginnig of userController
use App\Http\Controllers\UploadHandler;
and then have this method which creates instance of UploadHandler
public function file_uploader(Request $request)
{
$upload_handler = new UploadHandler();
}
create a controller named UploadHandler, add all the namespaces and other stuffs,
copy the class UploadHandler which is there in UploadHandler.php of the plugin. Now in this UploadHandler class contrsuctor you can call Your sessions(make sure to write use Session; in beginning of controller.
For deleting file,
update $file->deleteUrl object public function set_additional_file_properties($file) and it should look like below now
$file->deleteUrl= 'file_uploader?'
.$this->get_singular_param_name()
.'='.rawurlencode($file->name);
I am posting a gross answer above, You can do some workaround to make it accessible.
I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";
I am using the autocomplete plugin in Jquery as follows:
$(function(){
$("#username").autocomplete({
source: "http://localhost/websitename/index.php/admin/suggest_names" // path to the method in controller
});
});
The code for the text box is
<input type="text" name="username" id="username">
The suggest_names method in controller in turn calls a method in the model
I have tried providing the path to the method in controller by using
source: "admin/suggest_names"
in the above code.
However this is not working when the file is placed in a sub folder within the views folder application/views/admin. This works when the file is placed directly within the views folder.
I have tried using site_url but this does not work as well. (the form helper function is loaded in the administrator)
Can you suggest where I am making a mistake and suggest the syntax for the code to be used.
Stick to the following structure:
VIEW:
Your jquery function must be in a view file (it can be in the
Application/Views
directory or in a subdirectory of view). A view file is displayed in the browser and so the jquery code can run on client side. The controller is called from here. Your first code block example seems to be correct. You can also use: source: "/index.php/admin/suggest_names".
CONTROLLER:
A Controller (in your case called admin) is simply a class file that is named in a way that can be associated with a URI. They are placed in the
Application/Controllers
directory or a subdirectory of it. In your case you are calling the function suggest_names(), where you prepare the $data you want to output in the view file and call the database functions from a model (that last one is optional, you could treat the database functions from the controller as well).
MODELS:
Models are PHP classes that are designed to work with information in your database. They are placed in the
Application/Models
directory or subdirectory of it.