Unit 3: Controllers, Blade and Advanced Routing - Practice Quiz

INT221 — Mvc Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which Artisan command is used to generate a basic controller class in a Laravel/MVC environment?

A. php artisan create:controller UserController
B. php artisan generate:controller UserController
C. php artisan make:controller UserController
D. php artisan new:controller UserController

2 In a controller method, what is the primary mechanism for accessing data submitted via an HTTP POST request?

A. Using the $_POST superglobal directly
B. Reading from php://input
C. Type-hinting the Illuminate\Http\Request class
D. Using the Input::get() facade only

3 Which method is automatically called when a controller is instantiated, often used to register middleware?

A. register()
B. boot()
C. init()
D. __construct()

4 What is a Single Action Controller?

A. A controller containing only an __invoke method
B. A controller that can only handle GET requests
C. A controller that cannot use middleware
D. A controller that does not extend the base Controller class

5 When generating a Resource Controller, which flag is added to the artisan command?

A. --restful
B. --crud
C. --resource
D. --all

6 Which of the following methods is NOT included in an API Resource Controller (generated with --api)?

A. edit
B. index
C. destroy
D. store

7 Which routing statement creates routes for all seven standard resource actions at once?

A. Route::all('photos', PhotoController::class);
B. Route::controller('photos', PhotoController::class);
C. Route::crud('photos', PhotoController::class);
D. Route::resource('photos', PhotoController::class);

8 In the context of Controller Routing, what does the syntax [UserController::class, 'show'] represent?

A. A route group definition
B. The controller class and the specific method to execute
C. An array of middleware to apply
D. A resource definition

9 What file extension is strictly required for Blade templates?

A. .php
B. .blade
C. .blade.php
D. .tpl.php

10 Which Blade syntax is used to echo data while automatically escaping special characters to prevent XSS attacks?

A. <?= $data ?>
B. @echo($data)
C. {!! $data !!}
D. {{ $data }}

11 If you need to display unescaped HTML data in Blade, which syntax should be used?

A. {{-- $data --}}
B. @raw($data)
C. {{ $data }}
D. {!! $data !!}

12 Which directive is used to define a placeholder in a layout file that child views can inject content into?

A. @include
B. @extends
C. @yield
D. @section

13 Which directive is used at the beginning of a child view to specify which layout it inherits from?

A. @extends
B. @layout
C. @template
D. @parent

14 What is the correct syntax for a Blade comment that will not be rendered in the HTML source?

A. / Comment /
B. {{-- Comment --}}
C. // Comment
D. <!-- Comment -->

15 Inside a @foreach loop in Blade, what special variable is automatically available to check the current iteration state?

A. $index
B. $foreach
C. $iterator
D. $loop

16 Which Blade directive functions as the inverse of @if (executes if the condition is false)?

A. @empty
B. @else
C. @unless
D. @ifnot

17 How do you include a sub-view partials/header.blade.php inside another view?

A. @include('partials.header')
B. @yield('partials.header')
C. @import('partials.header')
D. @require('partials.header')

18 Which directive allows you to define a block of JavaScript that will remain untouched by the Blade engine (useful for JS frameworks)?

A. @verbatim
B. @raw
C. @javascript
D. @literal

19 When defining content for a section, what directive is used to append content to the parent's section rather than overwriting it?

A. @append
B. @super
C. @base
D. @parent

20 What is the primary benefit of using Named Routes?

A. They make the URL shorter.
B. They allow URL generation independent of the actual URI path.
C. They are processed faster by the server.
D. They automatically apply security headers.

21 How do you assign a name to a route definition?

A. Route::get('/path')->label('name');
B. Route::get('/path')->as('name');
C. Route::get('/path', ['name' => 'name']);
D. Route::get('/path')->name('name');

22 What is the correct syntax to define a route parameter constraint ensuring the id is numeric?

A. Route::get('/user/{id}')->validate('id', 'numeric');
B. Route::get('/user/{id}')->where('id', '[0-9]+');
C. Route::get('/user/{id}')->constraint('id', 'int');
D. Route::get('/user/{id}')->expect('id', '\d+');

23 Which method is used to define a group of routes that share common attributes like middleware or prefixes?

A. Route::group(...)
B. Route::batch(...)
C. Route::collection(...)
D. Route::bundle(...)

24 Consider the code: Route::prefix('admin')->group(...). If a route inside the group is /dashboard, what is the resulting URL?

A. /admin/dashboard
B. /admin-dashboard
C. /dashboard
D. /dashboard/admin

25 How do you define an optional parameter in a route?

A. Route::get('/user/{id_opt}')
B. Route::get('/user/{id?}')
C. Route::get('/user/[id]')
D. Route::get('/user/*id')

26 Which feature allows you to restrict a group of routes to a specific subdomain?

A. Route::host('api.example.com')
B. Route::domain('api.example.com')
C. Route::subdomain('api')
D. Route::url('api.example.com')

27 What helper function returns the full URL of the current request?

A. url()->now()
B. Route::current()
C. url()->current()
D. current_url()

28 How do you generate a URL to a specific application route named 'profile'?

A. link('profile')
B. href('profile')
C. route('profile')
D. url('profile')

29 When generating a URL to a named route that requires parameters, how are they passed?

A. route('profile?id=1')
B. route('profile', ['id' => 1])
C. route('profile', $id)
D. route('profile')->with('id', 1)

30 Which helper function is used to generate a URL for a file located in the public directory (like CSS or images)?

A. public()
B. file()
C. asset()
D. resource()

31 What is the purpose of Route::fallback()?

A. To redirect to the home page on error.
B. To handle database connection failures.
C. To define a route that is executed when no other route matches the incoming request.
D. To provide a backup controller for heavy loads.

32 To force a generated URL to use HTTPS, which helper function can be used?

A. url()->secure()
B. secure_url()
C. https_url()
D. ssl_route()

33 Which HTTP verb spoofing field is required in HTML forms to perform PUT or DELETE requests?

A. _token
B. _method
C. _action
D. _verb

34 What does the shortcut Route::view('/welcome', 'pages.welcome'); do?

A. It creates a controller that returns a view.
B. It redirects /welcome to pages.welcome.
C. It directly returns the view pages.welcome when /welcome is visited, without needing a dedicated controller.
D. It creates a view file.

35 What is the result of url()->previous()?

A. The URL of the home page.
B. The URL the user was at before the current request.
C. The parent directory of the current URL.
D. NULL if the user is not logged in.

36 Which directive can be used in Blade to check if the user is authenticated?

A. @check
B. @user
C. @auth
D. @if(logged_in)

37 In a Resource Controller, which method corresponds to the PUT/PATCH HTTP verb?

A. edit
B. save
C. update
D. store

38 What is Route Model Binding?

A. Creating routes directly inside Model classes.
B. Automatically injecting model instances into routes based on parameter IDs.
C. Restricting routes to specific models.
D. Binding a route to a specific database table manually.

39 If you want to define a route that redirects to another URI, which shortcut is most efficient?

A. Route::move('/here', '/there');
B. Route::forward('/here', '/there');
C. Route::get('/here', function() { return redirect('/there'); });
D. Route::redirect('/here', '/there');

40 In Blade, how can you output a variable as JSON, useful for passing data to JavaScript?

A. @js($data)
B. {{ json_encode($data) }}
C. @json($data)
D. {{ $data->toJson() }}

41 What is the purpose of the action() helper?

A. To generate a URL to a given controller action.
B. To perform a controller action immediately.
C. To create a form action attribute.
D. To validate a request action.

42 When using Route::group(['middleware' => ['auth']], ...) what happens?

A. The routes in the group are public.
B. The auth middleware is applied to all routes defined within the group.
C. The middleware is ignored.
D. It defines a middleware controller.

43 Which Blade control structure is used to check if a collection is empty before iterating?

A. @foreach ... @else ... @endforeach
B. @switch ... @case ... @endswitch
C. @while ... @empty ... @endwhile
D. @forelse ... @empty ... @endforelse

44 In a Controller, what does the helper view('home', ['name' => 'John']) do?

A. Returns a JSON response.
B. Redirects to the home route.
C. Compiles the 'home' view and passes the variable $name with value 'John' to it.
D. Creates a new view file named home.

45 How do you define a global pattern constraint for route parameters (e.g., ensuring id is always numeric across all routes)?

A. In the web.php file using Route::global()
B. In the RouteServiceProvider's boot method using Route::pattern()
C. In the Controller's constructor
D. In the .env file

46 Which syntax is used to stop the execution of a Blade @php block?

A. @done
B. @endphp
C. @end
D. @stop

47 If you have a named route photos.index which points to /photos, what does route('photos.index') return?

A. The string '/photos'
B. The controller method
C. The view file path

48 Which parameter constraint regex matches any alphanumeric character?

A. \w
B. [a-zA-Z0-9]+
C. [0-9]+
D. [a-z]+

49 In a Resource Controller, which method corresponds to the POST HTTP verb for creating a new item?

A. store
B. create
C. make
D. insert

50 What is the purpose of the Blade @stack directive?

A. To stack elements vertically in CSS.
B. To create a database transaction stack.
C. To render content pushed to a named stack from other views.
D. To debug the call stack.