I'm trying to learn the framework kohana.
I have defined a new controller under application/controller/classes. Which I named to hello.php:
class Controller_Hello extends Controller
{
public function action_say(){
$g = new View('firstv');
$g->render(TRUE);
}
}
?>
And I have this under application/views. Which I called firstv.php:
<h1>testing1</h1>
What's the mistake here. I'm using this guide:
http://pixelpeter.com/kohana/kohana101.pdf
I'm using the latest stable version 3.1.3.1. I have called the function by navigating to:
http://localhost/kohana/index.php/hello/say
Tried this using the same say function. And it worked. But this one doesn't use views.
$this->response->body('hello, world 2!');
Please help, thanks.
$this->response->body($g->render());
So your complete action method will be something like:
public function action_say()
{
$g = new View('firstv');
$this->response->body($g->render());
}
or:
public function action_say()
{
$g = new View('firstv');
$this->response->body($g);
}
or even:
public function action_say()
{
$this->response->body(new View('firstv'));
}
Related
i do my first steps in CI4.
Is there a way to define a modell into a controller for each functions of a controller one at one time?
At the moment i have to add the modell for each function as new like this
public function myfunction(){
$myModel = new \App\Models\MyModel();
}
public function myfunction_two(){
$myModel = new \App\Models\MyModel();
}
...
Update
Based on the answear i try this
class controllertwo extends BaseController
{
private $myModel;
public function __construct()
{
//Add some Models we need in this controller?
$this->myModel= new \App\Models\myModel();
//$this->myModel= new myModel();
}
...
The Codeigniter Errorconsole said "Use of undefined constant int - assumed 'int' (this will throw an Error in a future version of PHP) " and markes this line "$this->myModel= new \App\Models\myModel();"
What i do wrong? Or is it not the best way to include a modell for all functions inside a controller?
You can declare it as a private variable in the controller as follow :
private $model;
Then set it in the constructor as follow:
public function __construct()
{
$this->model = new \App\Models\MyModel();
}
Then simply refer to them in your methods as below:
public function myfunction(){
$this->model ...
}
public function myfunction_two(){
$this->model ...
}
How to call "functionA" from "ClassA" in "functionB" inside "ClassB" ?
class Base extends BaseController
{
public function header()
{
echo view('common/header');
echo view('common/header-nav');
}
}
class Example extends BaseController
{
public function myfunction()
// how to call function header from base class
return view('internet/swiatlowod');
}
}
Well there are many ways to do this...
One such way, might be like...
Assume that example.php is the required Frontend so we will need a route to it.
In app\Config\Routes.php we need the entry
$routes->get('/example', 'Example::index');
This lets us use the URL your-site dot com/example
Now we need to decide how we want to use the functions in Base inside Example. So we could do the following...
<?php namespace App\Controllers;
class Example extends BaseController {
protected $base;
/**
* This is the main entry point for this example.
*/
public function index() {
$this->base = new Base(); // Create an instance
$this->myfunction();
}
public function myfunction() {
echo $this->base->header(); // Output from header
echo view('internet/swiatlowod'); // Output from local view
}
}
When and where you use new Base() is up to you, but you need to use before you need it (obviously).
You could do it in a constructor, you could do it in a parent class and extend it so it is common to a group of controllers.
It's up to you.
Had already someone used Hashids in AdonisJs?
Being more specific, in a Model, to return a property hashid in an object
I'm working on a migration from Laravel to Adonis.
In Laravel, it's possible just with a couple of code lines in each Model, like this:
use Hashids;
class Menu extends Model
{
use \OwenIt\Auditing\Auditable;
protected $appends = ['hashid'];
public function getHashidAttribute()
{
return Hashids::encode($this->attributes['id']);
}
}
I installed this NPM package: https://www.npmjs.com/package/adonis-hashids, and I tried to figure out how to use like the Laravel way
I'd used Computed Properties (https://adonisjs.com/docs/4.1/database-getters-setters#_computed_properties)
class Menu extends Model {
static get computed () {
return ['hashids']
}
getHashids({ id }) {
return Hashids.encode(id)
}
}
I am new to Subsonic, and it seems that I cant find out a natural way to do CRUD operations using the LINQ template classes. I guess in ActiveRecord, you could:
Product p = new Product();
p.ProductCode = "xxx";
p.Add();
Using the LINQTemplate generated classes however, how can I do the same thing? I can only use something like this below to insert a product object:
db.Insert.Into<UnleashedSaaS.PRODUCT>(prod => prod.Code, prod => prod.Description).Values("Product1", "Product1 Desc").Execute();
Who could kindly give me some hints? I'd really appreciate it.
All the CRUD happens in SubSonicRepository, which you can derive from. For example, I would have a class like this:
public class ProductRepository : SubSonicRepository<Product> {
public ProductRepository() : base(new NorthwindDB()) { }
// need this here because base doesn't expose the DB class that I know of
protected NorthwindDB _db;
protected NorthwindDB DB {
get {
if (_db == null) _db = new NorthwindDB();
return _db;
}
}
public void Save(Product product) {
if (product.ProductId == 0) {
Add(product); // Add is part of SubSonicRepository
} else {
Update(product);
}
}
public void Delete(Product product) { ... }
public List<Product> ListAll() {
var products = from p in DB.Products
select p;
return products.ToList();
}
public Product GetById(int id) {
return DB.GetByKey(id);
}
}
And so on. It's nice because you can consolidate all your data access methods in one place. If you have Sprocs, they're generated as methods on DB as well.
When I get time I'm going to work on adding a Save method to SubSonicRepository directly so you don't have to do the check yourself to see which method (Add or Update) to call.
I have modified the Classes.tt file to include:
public partial class <#=tbl.ClassName#>Repository : SubSonicRepository<<#=tbl.ClassName#>>
{
public <#=tbl.ClassName#>Repository() : base(new <#=DatabaseName#>DB()) { }
}
Insert that bunch of lines between
<# foreach(Table tbl in tables){#>
and
/// <summary>
right at the top, near the namespace declaration, in my file it can be inserted in line 18.
The last thing to do is to add another "using" statement, in line 10, the next line after System.Linq statement. Now it should look like:
using System.Linq;
using SubSonic.Repository;
That will generate a repository to give you access to basic functionality, but can be modified in another partial class.
Hope that helps.
I'm new to SubSonic (of all flavours), but thought I might as well start with 3.0, because I'd like to use Linq, and I get the impression 3.0 is not that far away from release.
I tried the alpha download .zip, but that seems pretty old and didn't singularize table class names, so I'm now running from the latest trunk SVN version (rev62).
I've run the 'simple' templates, from SubSonic.Templates\Simple against my database and everything seems ok, but the DB context class which the templates create starts like this:
public partial class DB : IQuerySurface
{
static DB _db;
public DB() {
_db = new DB();
}
public static DB CreateDB()
{
if (_db == null)
{
_db = new DB();
_db.Init();
}
return _db;
}
... etc
Unsurprisingly, when I call DB.CreateDB, the ctor recurses endlessly and crashes everything with a stack overflow.
I don't really understand the ctor at all - it doesn't look like it should be there, but both the 'simple' and the 'advanced' templates create something similar, and there's a humongous test suite which I imagine is verifying this somehow.
Clearly I have the wrong end of the stick here - what blindingly obvious point have I missed?
Update: The simple and advanced templates aren't similar, and the advanced ones don't have this problem. Thanks for the help.
Another Update: It looks like this is fixed in the simple templates in SVN r66
Don't know if you have the latest bits from SVN with a bug, but my version from a few days ago appears to be working fine. Here is what my DB class starts off with:
public partial class DB : IQuerySurface
{
BatchQuery _batch = null;
public IDataProvider DataProvider;
public DbQueryProvider provider;
private IDatabaseSchema _schema;
public IDatabaseSchema Schema
{
get
{
return _schema;
}
}
public DB()
{
DataProvider = ProviderFactory.GetProvider("Northwind");
Init();
}
public DB(string instanceName, string connectStr)
{
SubSonic.DataProviders.ConnectionStringProvider.Instance.AddLocalConnectionString(
instanceName, connectStr, "System.Data.SqlClient");
DataProvider = ProviderFactory.GetProvider(instanceName);
Init();
}
... etc...
I used the advanced version of the templates.
I prefer the t4 templates, here is the ctor provided there:
public DB()
{
DataProvider = ProviderFactory.GetProvider("Northwind");
Init();
}
there is also an overload that accepts a connection string. This is working quite well for me, I'm using the linq support and it is full of awesome.